无法检查已有的用户名

时间:2016-01-04 09:34:50

标签: java mysql sql-server servlets

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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

@WebServlet(name = "SimpleServlet", urlPatterns = { "/SimpleServlet" })
public class SimpleServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

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

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

        String username = request.getParameter("username").toString();
        String password = request.getParameter("password").toString();
        String repassword = request.getParameter("repassword").toString();
        String ph = request.getParameter("phone").toString();
        double phone = Double.parseDouble(ph);

        String address = request.getParameter("address").toString();

        int status = register(username, password, repassword, phone, address);

        if (status > 0) {

            RequestDispatcher rd = request.getRequestDispatcher("Register_success.jsp");
            rd.include(request, response);
        } else {
            RequestDispatcher rd = request.getRequestDispatcher("Register_fail.jsp");
            rd.include(request, response);
        }

        out.close();
    }

    static int status = 0;

    @SuppressWarnings("null")
    public static int register(String username, String password, String repassword, double phone, String address) {

        PreparedStatement ps;
        Connection con;
        con = GetCon.getCon();
        try {
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT username FROM newuser where username='" + username + "'");

            while(rs.next())
            if (rs.getString("username").equalsIgnoreCase(username)) {
                System.out.println(username);
                HttpServletResponse response = null;
                response.sendRedirect("UserAlreadyExist.jsp");
            } else {

                ps = con.prepareStatement("Insert into newuser values(?,?,?,?,?)");
                ps.setString(1, username);
                ps.setString(2, password);
                ps.setString(3, repassword);
                ps.setDouble(4, phone);
                ps.setString(5, address);

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

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

            e.printStackTrace();
        }
        return status;

    }
}

您好,    我在执行上面的代码时遇到问题。当我们输入现有的用户名时,它会抛出一个NullPointerException。请有人帮我找错。代码适用于新用户名但不适用于现有用户名。谢谢!

1 个答案:

答案 0 :(得分:1)

response方法

register为空
HttpServletResponse response = null;   
response.sendRedirect("UserAlreadyExist.jsp");

使用response中的doPost(HttpServletRequest request, HttpServletResponse response)对象重定向用户。

您需要为register方法添加参数。

您的IDE会向帮助显示警告。请勿使用@SuppressWarnings("null")