严重:servlet [package] 的 Servlet.service() 在路径 [/portal] 的上下文中抛出异常 java.lang.NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35381693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:37:13  来源:igfitidea点击:

SEVERE: Servlet.service() for servlet [package] in context with path [/portal] threw exception java.lang.NullPointerException

javajspservletsmodel-view-controller

提问by lord-ivan

I have a login page and I want to redirect to a registration page.But I get HTTP Status 404 - /registration.jsperror. the path or project hierarchy: snapshot

我有一个登录页面,我想重定向到一个注册页面。但 HTTP Status 404 - /registration.jsp出现错误。路径或项目层次结构:快照

what i want is,if a user is not resisted they can register by clicking
what i am not doing right

我想要的是,如果用户没有被拒绝,他们可以通过点击
我做错的事情来注册

main.java

主程序

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

        response.getWriter().append("Served at: ").append(request.getContextPath());

        RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/Views/System/index.jsp");
        view.forward(request, response);

        String action = request.getParameter("action");
        if(action.equals("register"))
        {
           view = request.getRequestDispatcher("/WEB-INF/Views/System/registration.jsp");
           view.forward(request, response);
        } 
    }

index.jsp

索引.jsp

    <legend> Login to Jupiter </legend> 

            <table class="table" border="1" width="30%" cellpadding="3">
                <thead>
                    <tr>
                        <th colspan="2">Login Here</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>User Name</td>
                        <td><input type="text" name="username"  /></td> 
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" name="userpass"  /></td> 
                    </tr>

                    <tr>
                        <td colspan="2">New Employee!! <a class="btn btn-info" href="/registration.jsp?action=register">Register Here</a></td>

                    </tr>
                </tbody>
            </table>

            <input type="submit"  name="action" value="Login" />
             </fieldset> 
              </center>
        </form>
</body>
</html>

Error logs

错误日志

Feb 13, 2016 4:40:33 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.greensun.jupiter.controller.main] in context with path [/Jupiter] threw exception
java.lang.NullPointerException
    at com.greensun.jupiter.controller.main.doGet(main.java:32)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

采纳答案by Roman C

You constructed URL that redirects to not existing JSP page, but you should use URL that maps to a servlet acting as a controller.

您构建了重定向到不存在的 JSP 页面的 URL,但您应该使用映射到充当控制器的 servlet 的 URL。

<servlet-mapping>
    <servlet-name>MainServlet</servlet-name>
    <url-pattern>/main</url-pattern>
</servlet-mapping>

Now you should create a link

现在你应该创建一个链接

<a class="btn btn-info" href="${pageContext.request.contextPath}/main?action=register">Register Here</a>

In the controler you get parameter for registeraction and use dispatcher to forward to registration.jsp.

在控制器中,您获得操作参数register并使用调度程序转发到registration.jsp.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String action = request.getParameter("action");
    if(action != null && action.equals("register")) {
       RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/Views/System/registration.jsp");
       view.forward(request, response);
    } else {
      PrintWriter out = response.getWriter();
      out.print("Served at: "+request.getContextPath());    
      RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/Views/System/index.jsp");
      view.forward(request, response);
    }
}