ServletContextListener出错

时间:2014-06-10 18:04:05

标签: java servlets web nullpointerexception servletcontextlistener

我正在学习servlets,我从书中做了一个例子,但我得到了nullpointerexception

以下是我的课程:

package chala;

public class Dog {


private String breed;

public Dog(String breed) {
    this.breed = breed;
}

public String getBreed() {
    return breed;
}

public void setBreed(String breed) {
    this.breed = breed;
}

}

Dog class是 POJO 类,功能简单。

package chala;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class CtxListener implements ServletContextListener {

@Override
public void contextDestroyed(ServletContextEvent event) {
    ServletContext sc = event.getServletContext();
    String breed = sc.getInitParameter("breed");
    Dog d = new Dog(breed);
    sc.setAttribute("dog", d);

}

@Override
public void contextInitialized(ServletContextEvent event) {
    // TODO Auto-generated method stub

}

}

这是上下文监听器类,我从中获取servlet上下文对象,从中获取init参数,创建Dog对象,然后设置servlet上下文的属性。

package chala;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CtxListenerTester
 */
 @WebServlet("/CtxListenerTester")
public class CtxListenerTester extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("Servlet Context Listener Example");
    out.print("<BR/>");
    ServletContext sc = (ServletContext) getServletContext();
    System.out.println(sc.getAttribute("dog"));
    Dog dog = (Dog) getServletContext().getAttribute("dog");

    out.print("Dogs breed is :" + dog.getBreed());
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

}

这是我servlet我获取servlet上下文属性(DOG对象)并调用Dogs getBreed()方法的地方。当我开始发送GET请求时,我收到了nullpointerexception

这是我的web.xml:

<?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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>ContextListenerDemo</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>breed</param-name>
        <param-value>Labrador</param-value>
    </context-param>

    <listener>
        <listener-class>chala.CtxListener</listener-class>
    </listener>


</web-app>

1 个答案:

答案 0 :(得分:0)

您在contextDestroyed()方法中设置属性,该方法将在销毁上下文时调用,您需要在contextInitialized()

中设置该属性