Ola Pessoal!
É os seguinte:
criei uma servlet que implementa ServletContextListener
package servlet;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ControleDeAplicacao2
*/
public class ControleDeAplicacao2 extends HttpServlet implements ServletContextListener{
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ControleDeAplicacao2() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
public void contextDestroyed(ServletContextEvent event) {
}
@Override
public void contextInitialized(ServletContextEvent event) {
Date inicializacao = new Date();
ServletContext context = event.getServletContext();
context.setAttribute("inicializacao", inicializacao);
}
}
e criei outra servlet para testar a inicialização da primeira
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AcessaAplicacao
*/
public class AcessaAplicacao extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AcessaAplicacao() {
super();
// TODO Auto-generated constructor stub
}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext aplicacao = getServletContext();
Date inicial = (Date) aplicacao.getAttribute("inicializacao");
Date agora = new Date();
long diferenca = agora.getTime() - inicial.getTime();
double minutos = diferenca / (60 * 1000.0);
PrintWriter mostra = response.getWriter();
mostra.println("<html>");
mostra.println("hora inicial getTime: " + inicial.getTime());
mostra.println("hora atualgetTime: " + agora.getTime());
mostra.println("hora inicial: " + inicial);
mostra.println("hora atual: " + agora);
mostra.println("diferença: " + (agora.getTime() - inicial.getTime()));
mostra.println("minutos: " + minutos);
}
}
e mapiei no web.xml da seguinte maneira:
<?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>Web</display-name>
<servlet>
<servlet-name>AdicionaContatoServlet</servlet-name>
<servlet-class>servlet.AdicionaContatoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdicionaContatoServlet</servlet-name>
<url-pattern>/AdicionaContatoServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>teste1.jsp</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>java.sql.SQLException</exception-type>
<location>/erro.jsp</location>
</error-page>
<servlet>
<description></description>
<display-name>TestaParametro2</display-name>
<servlet-name>TestaParametro2</servlet-name>
<servlet-class>servlet.TestaParametro2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestaParametro2</servlet-name>
<url-pattern>/TestaParametro2</url-pattern>
</servlet-mapping>
<listener>
<listener-class>servlet.ControleDeAplicacao</listener-class>
</listener>
<servlet>
<description></description>
<display-name>AcessaAplicacao</display-name>
<servlet-name>AcessaAplicacao</servlet-name>
<servlet-class>servlet.AcessaAplicacao</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AcessaAplicacao</servlet-name>
<url-pattern>/AcessaAplicacao</url-pattern>
</servlet-mapping>
</web-app>
obs: já mapiei a outra servlet(ControleDeAplicacao2) e também não funciona.
Quando testo a url [link]http://localhost:8080/nomeDoMeuProjeto/AcessaAplicacao[/link] não funciona.
obs2: se eu tirar o <listener>
<listener-class>servlet.ControleDeAplicacao</listener-class>
</listener>
do web.xml tudo funciona, menos o link acima, e seu colocar o <listener>
<listener-class>servlet.ControleDeAplicacao</listener-class>
</listener>
nada funciona.
O que tem de errado?
Como eu faço pra dar certo?
desde já agradeço