如何使用request.getParameter(“ password_confirmation”)修复此NullPointerException?

时间:2019-05-13 16:57:51

标签: mysql jsp servlets netbeans

我一直试图从我的jsp表单调用servlet,到目前为止,它一直失败。它重定向到servlet的processRequest()并引发java.lang.NullPointerException:

13-May-2019 18:33:30.193 SEVERE [http-nio-8084-exec-252] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [index] in context with path [/RoyalAwards] threw exception
 java.lang.NullPointerException
    at index.doPost(index.java:85)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

13-May-2019 18:44:38.618 SEVERE [http-nio-8084-exec-252] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [index] in context with path [/RoyalAwards] threw exception
 java.lang.NullPointerException
    at index.doPost(index.java:85)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

我尝试使用action =“ $ {pageContext.request.contextPath} / index”,

我尝试确保表单按钮具有method =“ post”,

我尝试确保使用request.getParameter()引用正确的名称,

我试图寻找要删除的jsp-api-*。jar文件(在我的项目中不存在),

我尝试检查点击错误,并突出显示了if(password.equals(password_confirmation)){

jsp形式:

<form action="${pageContext.request.contextPath}/index" method="post" data-remote="true" accept-charset="UTF-8">
                                            <input type="radio" name="reg_type" value="sme" checked> SME <br>
                                            <input type="radio" name="reg_type" value="freelancer"> Freelancer <br>
                                            <input id="id" class="form-control" type="text" placeholder="Identification Nmber" name="id">
                                            <input id="email" class="form-control" type="text" placeholder="Email" name="email">
                                            <input id="password" class="form-control" type="password" placeholder="Password" name="password">
                                            <input id="password_confirmation" class="form-control" type="password" placeholder="Repeat Password" name="password_confirmation">
                                            <input class="btn btn-default btn-register" type="submit" value="Create account" name="submit">
                                        </form>

servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

        String password = request.getParameter("password");
        String password_confirmation = request.getParameter("password_confirmation");
        String reg_type = request.getParameter("reg_type");
        String id = request.getParameter("id");
        String email = request.getParameter("email");
        boolean check = false;

        if(password.equals(password_confirmation)){
            try{

                UserDAO udao = new UserDAO();
                check = udao.registerUser(id, email, password);

            } catch(Exception e){
                System.out.println(password);
                System.out.println(password_confirmation);
                System.out.println(reg_type);
                System.out.println(id);
                System.out.println(email);
                System.out.println(check);
                e.printStackTrace();
            }

            if(reg_type.equals("sme")){
                if (check) {
                    //response.sendRedirect("http://localhost:8084/RoyalAwards/sme reg/SME Application Form.jsp/#email="+email);
                    RequestDispatcher view = request.getRequestDispatcher("/sme reg/SME Application Form.jsp/#email="+email);
                    view.forward(request, response);
                }else{
                    //else 
                }
            }else{
                if (check) {
                    //response.sendRedirect("http://localhost:8084/RoyalAwards/freelancer reg/Freelancer Application Form.jsp/#email="+email);
                    RequestDispatcher view = request.getRequestDispatcher("/freelancer reg/Freelancer Application Form.jsp/#email="+email);
                    view.forward(request, response);
                }else{

                }
            }

        }else{

            // error

        }

    }

数据库方法:

public boolean registerUser(String identificationNumber, String email, String password) throws Exception {

        boolean check = false;

        try {

              String query = "insert into userinfo values('"+identificationNumber+"', '"+email+"', '"+password+"', 'pending')";

              PreparedStatement preparedStmt = con.prepareStatement(query);

              check = preparedStmt.execute();

              con.close();

              return check;

        }catch(Exception e) {
                    System.out.println(e.getLocalizedMessage());
                    throw e;
        }

    }

数据库连接字符串:

public UserDAO() throws Exception {

        try{  

            Class.forName("com.mysql.jdbc.Driver");  

            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pb","root","");

            stmt = con.createStatement();

        }catch(Exception e){ 

                    System.out.println(e.getLocalizedMessage());
                    throw e;

        }  
    }

成功后,我希望Web应用程序将数据插入数据库并重定向到另一个注册页面。

0 个答案:

没有答案