gpt4 book ai didi

java - ServletContextListener 错误

转载 作者:太空宇宙 更新时间:2023-11-04 14:48:06 25 4
gpt4 key购买 nike

我正在研究servlet,我从书中做了一个例子,但我得到了nullpointerexception

这是我的类(class):

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类是功能简单的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>

最佳答案

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

关于java - ServletContextListener 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24147946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com