简体   繁体   中英

Java Servlet Context Parameter Initialization Issues

I'm fairly new to java servlets and have been following this tutorial. I have several issues initializing parameters; all values retrieved from web.xml seem to be NULL. The code that throws the exception is:

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") returns the NULL string for every parameter value passed.

The web.xml file has the following entries:

<?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>

I am currently using eclipse kepler, jdk 7 and apache-tomcat-7.0.47 on a Win 7 x64 platform.

Edit:

The stacktrace:

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)

root cause

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)

I am not sure why you get org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) in the stacktrace. But if you are overriding init(config) try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM