当前位置: 首页 > 知识库问答 >
问题:

eclipse中的HttpServlet错误

孙佑运
2023-03-14
I started learning Java recently and I am facing lot of simple but irritating issues. I spent a lot of time trying to figure out the problem in vain.

I am trying to run a simple registration (with 3 pages) and submit the values into a DB. I am getting `NullPointerException` and not sure how to proceed with debugging any help will be greatly appreciated.

`form1.html`

Form details:

    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
        <form action="./registration" method="get">
                Name:<input type="text" name="name"><br/> 
        Father Name: <input type="text" name="fname"><br/> 
        Mother Name: <input type="text" name="mname"><br/> 
                     <input type="hidden" name="formd" value="1"/>
                     <input type="submit" value="Next>>>">
        </form>
    </body>
    </html>

`form2.html`

    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <form action="./registration">
        Contact:<input type="text" name= "contact"><br/>
        Email: <input type= "text" name= "email"><br/>
        Address: <input type ="text" name= "address"><br/>
        <input type="hidden" name= "formd" value="2"/>
        <input type= "submit" value="Next>>>">
    </form>
    </body>
    </html>

`form3.html`

    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <form action="./registration">
        Qualification:<input type="text" name= "qualification"><br/>
        Percentage: <input type ="text" name= "percentage"><br/>
        <input type="hidden" name= "formd" value="3"/>
        <input type= "submit" value="Submit!">
    </form>
    </body>
    </html>

**My Servlet**

    package controller;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

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

        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            PrintWriter out = response.getWriter();

            HttpSession httpsession = request.getSession();
            String fno = request.getParameter("formd");

            if (fno.equals("1")) {
                String name = request.getParameter("name");
                String fname = request.getParameter("fname");
                String mname = request.getParameter("mname");

                httpsession.setAttribute("name", name);
                httpsession.setAttribute("fname", fname);
                httpsession.setAttribute("mname", mname);

                response.sendRedirect("./form2.html");
            }

            if (fno.equals("2")) {
                String contact = request.getParameter("contact");
                String email = request.getParameter("email");
                String address = request.getParameter("address");

                httpsession.setAttribute("contact", contact);
                httpsession.setAttribute("email", email);
                httpsession.setAttribute("address", address);

                response.sendRedirect("./form3.html");

            }

            if (fno.equals("3")) {

                String qualification = request.getParameter("qualification");
                String percentage = request.getParameter("percentage");

                String name = (String) httpsession.getAttribute("name");
                String fname = (String) httpsession.getAttribute("fname");
                String mname = (String) httpsession.getAttribute("mname");

                String contact = (String) httpsession.getAttribute("contact");
                String email = (String) httpsession.getAttribute("email");
                String address = (String) httpsession.getAttribute("address");

                try {
                    Class.forName("oracle.jdbc.OracleDriver");
                    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:p1aor01", "system",
                            "sysdba");

                    PreparedStatement ps = conn.prepareStatement("insert into aadhar values (?,?,?,?,?,?,?,?)");
                    ps.setString(1, name);
                    ps.setString(2, fname);
                    ps.setString(3, mname);
                    ps.setString(4, contact);
                    ps.setString(5, email);
                    ps.setString(6, address);
                    ps.setString(7, qualification);
                    ps.setString(8, percentage);

                    int res = ps.executeUpdate();

                    if (res != 0) {
                        out.println("<font color='green'><h1>Registered Successfully!</h1>");
                    }

                } catch (SQLException e) {
                    out.println("<font color='red'><h1>Registration exception Failed!</h1>");
                    e.printStackTrace();
                } catch (ClassNotFoundException xe) {
                    xe.printStackTrace();
                }
            }
        }

    }

我忘记添加web.xml了。请看下文。如果我使用动态web模块3.1版,我读到我们不需要web.xml。我还能用吗?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd“id=”webapp_id“version=”2.5“>TestAadharRegistration AadharRegistration AadharRegistration Controller.AadharRegistration AadharRegistration/Registration

**Error:**

    java.lang.NullPointerException
        controller.AadharRegistration.doGet(AadharRegistration.java:26)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


    Note The full stack trace of the root cause is available in the server logs.

    From Console in eclipse:
    SEVERE: Servlet.service() for servlet [AadharRegistration] in context with path [/TestAadharRegistration] threw exception
    java.lang.NullPointerException
        at controller.AadharRegistration.doGet(AadharRegistration.java:26)

共有1个答案

姬选
2023-03-14

出于某种原因,这一行

 if (fno.equals("1")) {

正在引发NullPointerException。

这意味着表单没有发送值。即为空:

String fno = request.getParameter("formd");
if(fno != null && fno.equals("1")){
 类似资料:
  • 问题内容: 一切都很好,我能够运行这个JSP项目,突然发生了一些事情,并且大多数我的servlet都出现了无法解决的错误。 我知道这是因为找不到特定的 JAR 文件进行编译..但是我的“ buildpath ”很好,我没有进行任何更改。 我陷入这种情况… 试过了 多次清理项目 删除并添加了JRE库 删除并添加了服务器(Tomcat 7.0.23) 问题答案: 您必须将Web项目的运行时设置为正在使

  • 加载win32com:java.lang.unsatisfiedlinkerror:C:\program files\java\jre6\bin\win32com.dll:无法在AMD 64位平台上加载IA 32位.dll 编辑:我认为Java停止了对Java通信API的支持,但我不确定还能做什么来修复这个问题。有人能指出一个好的串行端口Java示例吗?我到处都找不到...

  • 今天,我试图为HttpServlet编写测试用例。在网络上。xml I使用param name和param value定义了一个init参数。我使用了JUnit测试用例。 通过调用这行代码: 并且将抛出异常: 我想是因为这些东西没有在Web容器中运行。令我惊讶的是,我找不到支持HttpServlet的测试套件。我找到了Apache仙人掌的StrutsTestCase或ServletTestCase

  • 2017/08/17 16:57:42 ERROR-jmeter.save.saveService:转换错误com.thoughtworks.xstream.conversionexception:com.atlantbh.jmeter.plugins.jsonutils.jsonpathextractor.jsonpathextractor:com.atlantbh.jmeter.jsonuti

  • Windows上的RAD 9.6.1(Java 8) 从Eclipse配置细节中,我看到: 但是改变那里的设置没有帮助。

  • 问题内容: 我在jsp文件中出现错误(在X行上),但这似乎都是正确的。 会是什么呢?是否在任何地方使用ServletException或javax.servlet.http? 问题答案: 对于第一个错误(),您需要将文件放入`classpath: 为此,请按照下列步骤操作: 右键单击该项目。 单击构建路径->配置构建路径 在库选项卡中->单击添加外部jar 选择档案 对于第二个错误:( ): 右键