JSP:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page errorPage="JspTest_error.jsp" %><html>
   <head>
     <title>JspTest</title>
   </head>
   <body>
     <%
        out.println("add attribute");
        getServletContext().setAttribute("userName", "hellking");
        
        out.println("replace attribute");
        getServletContext().setAttribute("userName", "shit");
        
        out.println("remove attribute");
        getServletContext().removeAttribute("userName");
     %>
   </body>
</html>
ServletListener:
package servletproject;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2011</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class ServletListenerTest extends HttpServlet implements
        ServletContextListener, ServletContextAttributeListener {
    
    private ServletContext context;    private void logout(String message) {
        PrintWriter out = null;        try {
            out = new PrintWriter(new FileOutputStream("c:\\test.txt", true));            out.println(new java.util.Date().toString() + "::Form ContextListener: " + message);            out.close();
        } catch(Exception e) {
            out.close();
            e.printStackTrace();
        }
    }    
    //Notification that the web module is ready to process requests
    public void contextInitialized(ServletContextEvent sce) {
        this.context = sce.getServletContext();        logout("contextInitialized()-->ServletContext 初始化了");        
        throw new java.lang.UnsupportedOperationException(
                "Method contextInitialized() not yet implemented.");
    }    //Notification that the servlet context is about to be shut down
    public void contextDestroyed(ServletContextEvent sce) {
        this.context = null;        logout("contextDestroyed()-->ServletContext 被销毁了");        
        throw new java.lang.UnsupportedOperationException(
                "Method contextDestroyed() not yet implemented.");
    }    //Notification that a new attribute has been added to the servlet context
    public void attributeAdded(ServletContextAttributeEvent scab) {
        logout("增加了一个 ServletContext 属性:attributeAdded(" + scab.getName() + ", " + scab.getValue() + ")");
        
        throw new java.lang.UnsupportedOperationException(
                "Method attributeAdded() not yet implemented.");
    }    //Notification that an attribute has been removed from the servlet context
    public void attributeRemoved(ServletContextAttributeEvent scab) {
        logout("删除了一个 ServletContext 属性:attributeRemoved(" + scab.getName() + ", " + scab.getValue() + ")");
        
        throw new java.lang.UnsupportedOperationException(
                "Method attributeRemoved() not yet implemented.");
    }    //Notification that an attribute of the servlet context has been replaced
    public void attributeReplaced(ServletContextAttributeEvent scab) {
        logout("某个 ServletContext 属性被改变:attributeReplaced(" + scab.getName() + ", " + scab.getValue() + ")");
        
        throw new java.lang.UnsupportedOperationException(
                "Method attributeReplaced() not yet implemented.");
    }
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <display-name>WebModule1</display-name>
  <listener>
    <listener-class>servletproject.ServletListenerTest</listener-class>
  </listener>
</web-app>为什么无论运行servletlistener还是jsp程序都显示为:
Help viewer error URL: Unable to open location: Document not found on server

解决方案 »

  1.   

    servlet还能这样用啊?没有doGet和dopost方法
      

  2.   

    这是Servlet的监听程序啊书上也没有doget,dopost的,而且运行了也没有错误显示的,只是显示Help viewer error URL: Unable to open location: Document not found on server,还有手动打开c:\test.txt里面却是这样的:
    Tue Sep 13 16:18:15 CST 2011::Form ContextListener: contextInitialized()-->ServletContext 初始化了
    Tue Sep 13 16:18:15 CST 2011::Form ContextListener: 某个 ServletContext 属性被改变:attributeReplaced(org.apache.catalina.WELCOME_FILES, [Ljava.lang.String;@16d2633)
    Tue Sep 13 16:18:15 CST 2011::Form ContextListener: 某个 ServletContext 属性被改变:attributeReplaced(org.apache.catalina.WELCOME_FILES, [Ljava.lang.String;@154864a)
    Tue Sep 13 16:18:15 CST 2011::Form ContextListener: 某个 ServletContext 属性被改变:attributeReplaced(org.apache.catalina.WELCOME_FILES, [Ljava.lang.String;@3c9217)
    Tue Sep 13 16:18:15 CST 2011::Form ContextListener: contextDestroyed()-->ServletContext 被销毁了
    为什么三个都是replace呢?我需要的结果应该是第一个是add,第二个是replace,第三个是remove的。。而且attributeReplaced(org.apache.catalina.WELCOME_FILES, [Ljava.lang.String;@16d2633),attributeReplaced(org.apache.catalina.WELCOME_FILES, [Ljava.lang.String;@154864a),attributeReplaced(org.apache.catalina.WELCOME_FILES, [Ljava.lang.String;@3c9217)中我需要的结果应该是("userName", "hellking"),("userName", "shit"),("userName")这样子的。。