Variáveis de ambiente no web.xml

10 respostas
M

Estou criando uma pagina para enviar email mas preciso que os dados do servidor SMTP sejam buscados como variáveis de ambiente (web.xml). Alguem sabe como eu faço isso ou conhecem um bom tutorial.

Valeu

10 Respostas

G

Olá,

Nesse caso não chamamos de variáveis de ambiente mas sim de Context Params.

Veja aqui um exemplo de como fazer:

http://www.java-tips.org/java-ee-tips/java-servlet/how-to-work-with-servletcontext-initilization-param-7.html

T

Voce ta usando algum servidor de aplicação?

M

sim o apache tomcat

T

TomCat é conteiner web, não um AS.
Veja a dica postada acima.

M

valeu Tchello
eu ja morei em Limeira-SP

M
package classes;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class ContextInitParameters extends HttpServlet {
	ServletConfig config;
	String emailRemet;
	String senha;

	public void init(ServletConfig config) throws ServletException {
		this.config = config;
		super.init(config);
	}

	public String getEmailRemet() {
		return emailRemet;
	}

	public void setEmailRemet(String emailRemet) {
		this.emailRemet = emailRemet;
	}

	public String getSenha() {
		return senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		ServletContext application = config.getServletContext();

		emailRemet = application.getInitParameter("emailRemet");
		senha = application.getInitParameter("senha");
		setEmailRemet(emailRemet);
		setSenha(senha);
		System.out.println(senha);
		System.out.println(emailRemet);

	}
}
M

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>EnviandoEmail</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>emailRemet</param-name> <param-value>[email removido]</param-value> </context-param> <context-param> <param-name>senha</param-name> <param-value>senhass</param-value> </context-param> <servlet> <description></description> <display-name>ContextInitParameters</display-name> <servlet-name>ContextInitParameters</servlet-name> <servlet-class>ContextInitParameters</servlet-class> </servlet> <servlet-mapping> <servlet-name>ContextInitParameters</servlet-name> <url-pattern>/ContextInitParameters</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>ContextInitParameter</display-name> <servlet-name>ContextInitParameter</servlet-name> <servlet-class>classes.ContextInitParameters</servlet-class> </servlet> <servlet-mapping> <servlet-name>ContextInitParameter</servlet-name> <url-pattern>/ContextInitParameter</url-pattern> </servlet-mapping> </web-app>

M

não esta dando certo :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning:

M
DEBUG: JavaMail version 1.4ea

DEBUG: java.io.FileNotFoundException: E:\Program Files\Java\jre6\lib\javamail.providers (O sistema não pode encontrar o arquivo especificado)

DEBUG: !anyLoaded

DEBUG: not loading resource: /META-INF/javamail.providers

DEBUG: successfully loaded resource: /META-INF/javamail.default.providers

DEBUG: Tables of loaded providers

DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}

DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}

DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map

DEBUG: !anyLoaded

DEBUG: not loading resource: /META-INF/javamail.address.map

DEBUG: java.io.FileNotFoundException: E:\Program Files\Java\jre6\lib\javamail.address.map (O sistema não pode encontrar o arquivo especificado)

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]

DEBUG SMTP: useEhlo true, useAuth true

DEBUG SMTP: useEhlo true, useAuth true
M

:frowning:

Criado 1 de setembro de 2009
Ultima resposta 1 de set. de 2009
Respostas 10
Participantes 3