gpt4 book ai didi

Java Servlet 上下文参数初始化问题

转载 作者:行者123 更新时间:2023-11-28 23:39:54 24 4
gpt4 key购买 nike

我是 java servlet 的新手,一直在关注 this教程。我有几个初始化参数的问题;从 web.xml 检索到的所有值似乎都是 NULL。抛出异常的代码是:

public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public void init() throws ServletException {
//we can create DB connection resource here and set it to Servlet context
System.out.println("Check the string getServletContext().getInitParameter(\"dbURL\") = " + getServletContext().getInitParameter("dbURL")); // <== exception is thrown at this line
if(getServletContext().getInitParameter("dbURL").equals("jdbc:mysql://localhost/mysql_db") &&
getServletContext().getInitParameter("dbUser").equals("mysql_user") &&
getServletContext().getInitParameter("dbUserPwd").equals("mysql_pwd"))
getServletContext().setAttribute("DB_Success", "True");
else throw new ServletException("DB Connection error");
}
...
}

getServletContext.getInitParameter("anyParameter") 为传递的每个参数值返回 NULL 字符串。

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>DynamicWebProject1</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>login.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>dbURL</param-name>
<param-value>jdbc:mysql://localhost/mysql_db</param-value>
</context-param>
<context-param>
<param-name>dbUser</param-name>
<param-value>mysql_user</param-value>
</context-param>
<context-param>
<param-name>dbUserPwd</param-name>
<param-value>mysql_pwd</param-value>
</context-param>
</web-app>

我目前在 Win 7 x64 平台上使用 eclipse kepler、jdk 7 和 apache-tomcat-7.0.47。

编辑:

堆栈跟踪:

javax.servlet.ServletException: Servlet.init() for servlet sebi.first.LoginServlet threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

根本原因

java.lang.NullPointerException
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:125)
sebi.first.LoginServlet.init(LoginServlet.java:32)
javax.servlet.GenericServlet.init(GenericServlet.java:160)

org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)

org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

最佳答案

我不确定你为什么得到 <code>org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)</code>在堆栈跟踪中。但是如果你重写 init(config) 试试这个:



<pre><code>public void init(ServletConfig config) throws ServletException {
super.init(config);
/* and rest of the things here */

}
</code></pre>

关于Java Servlet 上下文参数初始化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20825271/

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