servlet:服务器遇到内部错误,导致无法完成此请求

时间:2015-04-05 11:55:53

标签: java jsp servlets

我用jsp页面和servlet创建了注册页面,我得到了错误 description服务器遇到内部错误,导致无法完成此请求。

exception

java.lang.NullPointerException
    com.javatpont.RegisterUser.register(RegisterUser.java:13)
    com.javatpont.CreateServlet.doGet(CreateServlet.java:33)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是---> Register.jsp页面

<form  name=F1 onSubmit="return dil(this)" action="CreateServlet">
    <table height="350" cellspacing="5" cellpadding="3">
        <tr>
            <td>USER NAME:</td><td> <input type="text" name="username"/></td>
        </tr>
        <tr>
            <td>PASSWORD:</td><td> <input type="password" name="password"/></td>
        </tr>
        <tr>
            <td>RE-PASSWORD:</td><td> <input type="password" name="repassword"/></td>
        </tr>
        <tr>
            <td>PHONE:</td><td> <input type="text" name="phone"/></td>
        </tr>
        <tr>
            <td>ADDRESS:</td><td> <textarea rows=4 cols=40 wrap=virtual name="adderess"></textarea></td>
        </tr>
        <TR>
            <TD> Select your designation:</TD> 
            <TD> 
                <SELECT NAME="syd"> 
                    <option>student
                    <option>professor
                    <option>Associate-Professor
                    <option>lecturer
                    <option>non-teaching staff
                </SELECT> 
            </TD>
        </TR>
        <tr><td></td><td><input type="submit" value="Submit"/>
        <INPUT TYPE=RESET VALUE="CLEAR"></td></tr>
    </table>
</form>

这是CreateServlet.java

    package com.javatpont;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.rmi.Naming;

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



    public class CreateServlet extends HttpServlet {

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

            response.setContentType("text/html");
            PrintWriter out=response.getWriter();

            String username=request.getParameter("username");
            System.out.println("-----------------------"+username);
            String password=request.getParameter("password");
            String repassword=request.getParameter("repassword");
            String ph=request.getParameter("phone");
            double phone=Double.parseDouble(ph);

            String adderess=request.getParameter("adderess");

            String syd=request.getParameter("syd");

   // line number 33        int status=RegisterUser.register(username, password, repassword,phone, adderess,syd);
            System.out.println("***********************"+username);

            if(status>0){
                System.out.println("==========================="+username); 
                request.setAttribute("welcome","WELCOME! YOU HAVE BEEN REGISTERD");
                RequestDispatcher rd=request.getRequestDispatcher("member.jsp");
                rd.include(request, response);
            }
            else{
                out.print("Sorry,Registration failed. please try later");
                RequestDispatcher rd=request.getRequestDispatcher("member.jsp");
                rd.include(request, response);
            }

        out.close();    
        }

    }

这是RegisterUser.java

 package com.javatpont;
    import java.sql.*;
    public class RegisterUser {
    static int status=0;
    //int accountno=1;
    public static int register(String username,String password,String repassword,double phone,String adderess,String syd){
        //public static int register(String email,String password,String gender,String country,String name){

        Connection con=GetCon.getCon();
        PreparedStatement ps;

        try {
System.out.println("here is the problem line number 13");
  //line number13->         ps = con.prepareStatement("Insert into NEWMEMBER values(?,?,?,?,?,?,?)");
            int nextvalue1=GetCon.getPrimaryKey();
            ps.setInt(1,nextvalue1);
            ps.setString(2,username);
            ps.setString(3,password);
            ps.setString(4,repassword);
            ps.setDouble(5,phone);
            ps.setString(6,adderess);
            ps.setString(7,syd);

            status=ps.executeUpdate();
            System.out.println(status);
        } catch (SQLException e) {

            e.printStackTrace();
        }
        return status;

    }
    }

这是 - &gt; GetCon.java

public class GetCon {
private GetCon(){}

public static Connection con;
static{
    try {
        Class.forName(DBIntializer.DRIVER);
        con=DriverManager.getConnection(DBIntializer.CON_STRING,DBIntializer.USERNAME,DBIntializer.PASSWORD);
    } catch (ClassNotFoundException e) {

        e.printStackTrace();
    } catch (SQLException e) {

        System.out.println("Exception in GetCon");
    }

}
public static Connection getCon(){
    return con;
}



public static int getPrimaryKey(){
    int nextvalue=0;
    Connection con=GetCon.getCon();
    PreparedStatement ps2;
    try {

    ps2=con.prepareStatement("select ff from dual");

    ResultSet rs=ps2.executeQuery();
    rs.next();
    nextvalue=rs.getInt(1);



} catch (SQLException e) {

        e.printStackTrace();
    }
return nextvalue;

}
}

1 个答案:

答案 0 :(得分:0)

嗨,我认为你的实际问题在这里

Connection con=GetCon.getCon();

您可以获得连接并仍然尝试这样做..

ps = con.prepareStatement(...)

我们需要先找出连接失败的原因..检查你给出的URL就像这样“jdbc:mysql:// localhost / DATABASE_NAME”.. localhost如果db在你的机器本身或者那个ip ..还要确保你在classpath中有所需的jdbc驱动程序..

编辑:

看到最新的评论和代码,它看起来像是一个丢失的jar问题..你需要在Runtime类路径中添加一个连接器库`

java -cp .;mysql-connector-java-5.1.25-bin.jar GetCon` 

我的示例使用Windows类路径分隔符“;”,在其他系统上它可能不同(Linux上的“:”)。它还假设,mysql-connector-java-5.1.25-bin.jar位于同一文件夹中。如果不是这种情况,则将路径放入库而不是普通名称。