简单的用户登录页面与后台数据库的交互

思路为:当用户登录时,如果用户名和密码都正确的时候会进入成功页面。在validate.jsp里会先获得用户名,然后再数据里查找此用户名是否已经存在,若存在则再与所对应的密码进行对比,若都相同则返回flag为2,成功登录,若用户名存在而密码不正确,则返回flag为1,提示密码错误;若在数据库里找不到对应的用户名,则返回flag为0,提示用户名错误。
代码实现部分:
login.jsp

<body>
    <form action="validate.jsp" method="post">
    <table>
        <tr>
            <td>用户名:</td>
            <td><input type="text" name="loginName"></td>
            <td><font color="red"><%=request.getAttribute("loginNameError")==null ? "":request.getAttribute("loginNameError") %></font></td>
        </tr>
        <tr>
            <td> 密码:</td>
            <td><input type="password" name="loginPass"></td>
            <td><font color="red"><%=request.getAttribute("loginPassError")==null?"":request.getAttribute("loginPassError") %></font></td>
        </tr>
        <tr>
            <td><input type="submit" value="提交"></td>
            <td><input type="reset" value="重置"></td>
        </tr>
     </table>
    </form>
</body>

validate.jsp

<body>
   <%
    String loginName=request.getParameter("loginName");
    String loginPass=request.getParameter("loginPass");

    User user=new User();
    user.setUsername(loginName);
    user.setPassword(loginPass);

    LoginDao loginDao=new LoginDaoImpl();
    int flag=loginDao.login(user);
    if(flag==2){
        session.setAttribute("loginName", loginName);
        response.sendRedirect(request.getContextPath()+"/index.jsp");
    }else {
        if(flag==1){
            request.setAttribute("loginPassError", "loginPass is error");
        }else if(flag==0){
            request.setAttribute("loginNameError", "loginName is error");
        }
        request.getRequestDispatcher("/login.jsp").forward(request, response);
    }
    %>
  </body>

//编写一个接口,只定义方法,具体实现让实现它的接口去完成。
LoginDao.java

package com.dao.dao;

import com.jdbc.jdbc.User;

public interface LoginDao {
    int login(User user);
}

//实现接口的类,实现了具体的操作
LoginDaoImpl.java

package com.impls.impls;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.jdbc.common.ConnectionUtils;
import com.dao.dao.LoginDao;
import com.jdbc.jdbc.User;

public class LoginDaoImpl implements LoginDao{
    Connection con=null;
    PreparedStatement pstm=null;
    ResultSet rs=null;

    public int login(User user) {
        //连接数据库
        con=ConnectionUtils.getConnection();
        int flag=0;
        try {
            //发送sql文
            String sql="select t_name,t_pass from t_user where t_name=?";
            pstm=con.prepareStatement(sql);
            pstm.setString(1, user.getUsername());
            rs=pstm.executeQuery();
            //处理结果集
            while(rs.next()){
                String temp=rs.getString("t_pass");
                if(temp.equals(user.getPassword())){
                    flag=2; //用户名和密码都正确
                }else{
                    flag=1;//密码错误
                }

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            //资源释放(逆序释放)
            ConnectionUtils.close(rs, pstm, con);   
        }   
        return flag;
    }
}

具体的公共类ConnectionUtils.java可以参考JDBC连接数据库及具体代码实现

在编写jsp登录页面,将页面输入的用户名和密码进行与sql数据库验证,正确跳转到success.jsp,否则跳转到error.jsp页面,但是写完后不管输入正确的用户名还是错误的,都是跳转到错误页面,下面是console页面的打印信息,求大神帮我看看哪一步出现了错误,谢谢啦 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1892) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1735) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.weiye.util.ConnectionFactory.makeConnection(ConnectionFactory.java:45) at com.weiye.service.CheckUserService.check(CheckUserService.java:19) at com.weiye.servlet.CheckServlet.doPost(CheckServlet.java:57) at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at com.weiye.filter.FilterTwo.doFilter(FilterTwo.java:37) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at com.weiye.filter.FilterOne.doFilter(FilterOne.java:45) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1083) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:640) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) java.lang.NullPointerException at com.weiye.service.CheckUserService.check(CheckUserService.java:20) at com.weiye.servlet.CheckServlet.doPost(CheckServlet.java:57) at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at com.weiye.filter.FilterTwo.doFilter(FilterTwo.java:37) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at com.weiye.filter.FilterOne.doFilter(FilterOne.java:45) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1083) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:640) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) java.lang.NullPointerException at com.weiye.service.CheckUserService.check(CheckUserService.java:32) at com.weiye.servlet.CheckServlet.doPost(CheckServlet.java:57) at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.Chain.doFilter(ApplicationFilterChain.java:208)
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页