gpt4 book ai didi

Java 小服务程序 : getInitParameter not work in Service()

转载 作者:行者123 更新时间:2023-11-30 06:37:47 24 4
gpt4 key购买 nike

我在我的 web.xml 配置文件中添加了一些参数,如下所示:

  <context-param>
<param-name>service1</param-name>
<param-value>http://www.example.com/example2.html</param-value>
</context-param>
<context-param>
<param-name>service2</param-name>
<param-value>http://www.example.com/example2.html</param-value>
</context-param>
...

我尝试在我的 servlet 中获取参数,特别是在我的服务方法中:

protected void service(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
System.out.println(this.getServletContext().getInitParameter("service1"));
...

但是在运行时我有一个 NullPointerException...我怎样才能得到包含在 web.xml 中的参数值?

这是堆栈跟踪:

GRAVE: Servlet.service() for servlet DispatcherServlet threw exception
java.lang.NullPointerException
at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:160)
at it.servlethope.DispatcherServlet.service(DispatcherServlet.java:66)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

DispatcherServlet.java:66 是我尝试 getInitParameter() 的行

最佳答案

如果调用 getServletContext()抛出 NPE 则意味着 ServletConfig 为空。

因此,您已经在自己的 servlet 中覆盖了 init(ServletConfig config),并且您没有按照 Javadoc 正确调用 super.init(config) :

Called by the servlet container to indicate to a servlet that the servlet is being placed into service. See Servlet.init(javax.servlet.ServletConfig).

This implementation stores the ServletConfig object it receives from the servlet container for later use. When overriding this form of the method, call super.init(config).

相应地修复它:

public void init(ServletConfig config) throws ServletException {
// Do your thing here.
// ...
// And then finally:
super.init(config);
}

或者直接使用无参数的init()方法,不需要ServletConfig,这样你就不用担心了。

关于Java 小服务程序 : getInitParameter not work in Service(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3036855/

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