尝试注册帐户时为什么会出现错误?

时间:2020-07-10 02:03:39

标签: java mysql spring jsp servlets

请帮助我如何在Java Web应用程序上解决此注册问题。我试图做这7天,但我没有办法。再次请帮助!我想我已经设置了您需要帮助我的一切。只是当我想注册时,这个困扰了我很长一段时间的问题就把我拒之门外。就像我说的那样,我已经尝试解决了七天,但是我做得不太好。我来这里是为了找一个可以帮助我解决这个问题的好人。我将非常感谢他!

User.java:

{
    package model;

public class User {
    private int privateID;
    private String firstname;
    private String lastname;
    private String username;
    private String password;
    
    public User() {
        
    }
    
    public User(int privateID, String firstname, String lastname, String username, String password) {
        this.privateID = privateID;
        this.firstname = firstname;
        this.lastname = lastname;
        this.username = username;
        this.password = password;
    }

    public int getPrivateID() {
        return privateID;
    }

    public void setPrivateID(int privateID) {
        this.privateID = privateID;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getPassword() {
        return password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

DBConnect.java:


package dao;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnect {

    public static Connection getConnecttion() {
        Connection cons = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            cons = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/medical", "root", "root");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cons;
    }
}

UserDAO.java:


package dao;
import model.User;

public interface UserDAO {
    
    public void addUser(User u);
    
    public boolean checkUser(String username);
    
    public boolean login(String username, String password);
    
    public void updateUser(User u);
    
    public User getUser(String username);
    
}

UserDAOImpl.java:


package dao;

import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.User;

public class UserDAOImpl implements UserDAO {

    @Override
    public void addUser(User u) {
        Connection con = DBConnect.getConnecttion();
        String sql = "insert into user value (?,?,?,?,?)";
        PreparedStatement ps;

        try {
            ps = (PreparedStatement) con.prepareStatement(sql);
            ps.setInt(1, u.getPrivateID());
            ps.setString(2, u.getFirstname());
            ps.setString(3, u.getLastname());
            ps.setString(4, u.getUsername());
            ps.setString(5, u.getPassword());
            ps.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public boolean checkUser(String username) {
        Connection con = DBConnect.getConnecttion();
        String sql = "select * from user where username='" + username + "'";
        PreparedStatement ps;

        try {
            ps = (PreparedStatement) con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                con.close();
                return true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean login(String username, String password) {
        Connection con = DBConnect.getConnecttion();
        String sql = "select * from user where username='" + username + "' and password='" + password + "'";
        PreparedStatement ps;

        try {
            ps = (PreparedStatement) con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                con.close();
                return true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    @Override
    public void updateUser(User u) {
        Connection con = DBConnect.getConnecttion();
        String sql = "update user set privateID=?, firstname=?, lastname=?, username=?, password=?";

        try {
            PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql);
            ps.setInt(1, u.getPrivateID());
            ps.setString(2, u.getFirstname());
            ps.setString(3, u.getLastname());
            ps.setString(4, u.getUsername());
            ps.setString(5, u.getPassword());
            ps.executeUpdate();
            con.close();

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

    @Override
    public User getUser(String name) {
        Connection con = DBConnect.getConnecttion();
        String sql = "select * from user where username='"+name+"'";
        User u = new User();
        
        try {
            PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                int privateID = rs.getInt("privateID");
                String firstname = rs.getString("firstname");
                String lastname = rs.getString("lastname");
                String username = rs.getString("username");
                String password = rs.getString("password");
                
                u = new User(privateID, firstname, lastname, username, password);
            }
            con.close();
        } catch (SQLException ex) {
          ex.printStackTrace();
        }
        return u;
    }
}

RegisterServlet.java:


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controler;

import dao.UserDAOImpl;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.User;

/**
 *
 * @author Veljko
 */
public class RegisterServlet extends HttpServlet {
    private UserDAOImpl userDAO = new UserDAOImpl();
    
    
    public RegisterServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        
        String firstname = req.getParameter("firstname");
        String lastname = req.getParameter("lastname");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        
        
        String err = "";
        String url = "/register.jsp";
        
        
        if(firstname.equals("") || lastname.equals("") || username.equals("") || password.equals("")) {
            err+="Potrebno je popuniti sva polja!";
        } else {
            if(userDAO.checkUser(username) == true) {
                err+="Username vec postoji!";
            } 
        }
        if(err.length()>0) {
            req.setAttribute("err", err);
        }
        
        try {
            if (err.length() == 0) {
                userDAO.addUser(new User(0, firstname, lastname, username, password));
                userDAO.login(username, password);
                Cookie loginCookie = new Cookie("username", username);
                loginCookie.setMaxAge(30*60);
                resp.addCookie(loginCookie);
                resp.sendRedirect("index.jsp");
            } else {
                url = "/register.jsp";
                RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
                rd.forward(req, resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resp.sendRedirect("/register.jsp");
        }
    }        
}

register.jsp:

    
<%@include file="header.html" %>
<body>
    <%

    String err = "";
    if(request.getAttribute("err") !=null) {
        err = (String) request.getAttribute("err");
    }

        %>
    
    <main>
        <div class="container register">
            <div class="row">
                <div class="col-md-3 register-left">
                    <img src="https://image.ibb.co/n7oTvU/logo_white.png" alt=""/>
                    <h3>Dobrodosli</h3>
                <a href="login.jsp" class="btn -btn">Login</a>
                </div>
                <div class="col-md-9 register-right">
                    <div class="tab-content" id="myTabContent">
                        <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
                            <h3 class="register-heading">Registracija</h3>
                            <div class="row register-form">
                                <div class="col-md-6">
                                    <form action="RegisterServlet" method="post">
                                        <li style="color: red"><%=err%></li>
                                        <input type="text" class="form-control" name="firstname" placeholder="First Name *" value="" />
                                        <input type="text" class="form-control" name="lastname" placeholder="Last Name *" value="" />
                                        <input type="text" class="form-control" name="username" placeholder="Username *" value="" />
                                        <input type="password" class="form-control" name="password" placeholder="Password *" value="" />
                                        <input type="submit" class="btnRegister"  value="Register"/>
                                        </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </main>
<%@include file="footer.html" %>

错误:

10-Jul-2020 03:53:06.169 SEVERE [http-nio-8084-exec-69] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [RegisterServlet] in context with path [/ITAcademy] threw exception
java.lang.NullPointerException
at dao.UserDAOImpl.checkUser(UserDAOImpl.java:40)
at controler.RegisterServlet.doPost(RegisterServlet.java:54)
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)

1 个答案:

答案 0 :(得分:0)

试图在UserDAOImpl.java第40行使用的con错误日志为空。 追溯到类DBConnect,获取连接时应该发生错误。 (仅检查catch块打印的堆栈轨迹。 您是否缺少mysql驱动程序?

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver