Eclipse和Tomcat错误500

时间:2018-06-29 10:13:39

标签: java eclipse tomcat

我已经创建了一个注册表格。 当我执行home.jsp并尝试进行注册时,出现这种错误: HTTP 500状态

    Type Exception Report

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

java.lang.NullPointerException
    it.DBQuery.unique(DBQuery.java:59)
    it.Registration.doGet(Registration.java:43)
    it.Registration.doPost(Registration.java:58)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

这是我的 Registration.java

代码
package java;
        import java.io.IOException;
        import javax.servlet.ServletException;
        import javax.servlet.annotation.WebServlet;
        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import javax.servlet.http.HttpSession;

        /**
         * Servlet implementation class Registration
         */
        @WebServlet({ "/registration" })
        public class Registration extends HttpServlet {
            private static final long serialVersionUID = 1L;

            /**
             * @see HttpServlet#HttpServlet()
             */
            public Registration() {
                super();
                // TODO Auto-generated constructor stub
            }

            /**
             * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
             */
            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                // TODO Auto-generated method stub
                DBQuery DB = new DBQuery();
                String login = request.getParameter("login");
                String password = request.getParameter("password");
                String firstname = request.getParameter("firstname");
                String lastname = request.getParameter("lastname");

                HttpSession session=request.getSession();
                session.setAttribute("Login", login);
                session.setAttribute("FirstName", firstname);
                session.setAttribute("LastName", lastname);

                if(DB.unique(login)){
                    DB.register(login, password, firstname, lastname);
                    session.setAttribute("Logged-in", "true");
                    response.sendRedirect("home.jsp");
                } else {
                    session.setAttribute("ErrorMessage", "Username already in use!");
                    response.sendRedirect("registration.jsp");
                }
            }

            /**
             * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
             */
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                // TODO Auto-generated method stub
                doGet(request, response);
            } }

DBQuery.java文件:

package java;

    import java.sql.*;

    public class DBQuery {
        private String connectionURL = "jdbc:postgresql://localhost:5432/SitoDB";
        private Connection connection = null;
        private PreparedStatement statement = null;
        private ResultSet rs = null;

        private String query = "select * from users where login=?";
        private String insert = "insert into users(login, firstname, lastname, password) values (?,?,?,?)";

        public DBQuery() {
            // TODO Auto-generated constructor stub
            // get a DB connection
            try{
                Class.forName("com.postgresql.Driver").newInstance();
                connection = DriverManager.getConnection(connectionURL, "postgres", "12345");
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }

        @Override
        protected void finalize() {
            try{
                connection.close();
            }
            catch(SQLException e){
                e.printStackTrace();
            }           
        }

        public boolean validate(String login, String password){
            boolean result = false;
            try{
                statement = connection.prepareStatement(query);
                statement.setString(1, login);
                rs = statement.executeQuery();

                if (rs.next() && password.equals(rs.getString("password")))
                    result = true;

                rs.close();
                statement.close();
            }
            catch(SQLException e){
                e.printStackTrace();
            }

            return result;
        }

        public boolean unique(String login){
            boolean result = false;
            try{
                statement = connection.prepareStatement(query);
                statement.setString(1, login);
                rs = statement.executeQuery();

                if (rs.next())
                    result = false;
                else
                    result = true;

                rs.close();
                statement.close();
            }
            catch(SQLException e){
                e.printStackTrace();
            }

            return result;
        }

        public void register(String login, String password, String firstname, String lastname){
            // should write data to DB table
            try{
                statement = connection.prepareStatement(insert);
                statement.setString(1, login);
                statement.setString(2, firstname);
                statement.setString(3, lastname);
                statement.setString(4, password);
                statement.executeUpdate();

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

        public String getFirstName(String login){
            String result="";
            try{
                statement = connection.prepareStatement(query);
                statement.setString(1, login);
                rs = statement.executeQuery();

                if (rs.next()) 
                    result = rs.getString("firstname");
                else
                    result = "";

                rs.close();
                statement.close();
            }
            catch(SQLException e){
                e.printStackTrace();
            }

            return result;
        }

        public String getLastName(String login){
            String result="";
            try{
                statement = connection.prepareStatement(query);
                statement.setString(1, login);
                rs = statement.executeQuery();

                if (rs.next()) 
                    result = rs.getString("lastname");
                else
                    result = "";

                rs.close();
                statement.close();
            }
            catch(SQLException e){
                e.printStackTrace();
            }
            return result;
        }
    }

在我的数据库(使用PostgreSQL创建)中,我用登录名,密码,名,姓创建了一个USERS表。 我怎么解决这个问题?谢谢你们。

0 个答案:

没有答案