Java web登录验证

bean层(封装数据对象):

package bean;

public class LoginBean 
{
  private String name;
  private String pwd;
  
  public LoginBean() 
  {
    super();
  }
  public LoginBean(String name, String pwd) 
  {
    super();
    this.name = name;
    this.pwd = pwd;
  }
  public String getName()
  {
     return name;
  }
 public void setName(String name) 
 {
    this.name = name;
 }
 public String getPwd() {
    return pwd;
 }
 public void setPwd(String pwd) 
 {
    this.pwd = pwd;
 }
  
}

dao层(封装对数据的逻辑操作):

package dao;

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

import bean.LoginBean;


//处理登录
public class LoginDao 
{
   private final static String DriverName="com.mysql.jdbc.Driver";
   private final static String Uname="root";
   private final static String Upwd="1108.shjzh..sql.lq";
   private final static String DbURL="jdbc:mysql://localhost:3306/login";
  public static int Connect(LoginBean loginbean)
  {
      int  flag=-1;   //flag=1(登录成功)
      Connection conn=null;
      PreparedStatement pstmt=null;
      ResultSet result=null;
      int count=-1;
      try {
          Class.forName(DriverName);
          conn=DriverManager.getConnection(DbURL, Uname, Upwd);
          String sql="select count(*) from user where name=? and pwd=?";
          pstmt=conn.prepareStatement(sql);
          pstmt.setString(1, loginbean.getName());
          pstmt.setString(2, loginbean.getPwd());
          result=pstmt.executeQuery();
          if(result.next())
          {
              count=result.getInt(1);
          }
          if(count>0)
          {
              return 1;    //登录成功
          }
          else
          {
              return 0;  //登录失败(用户名,密码有误)
          }
      }
      catch(ClassNotFoundException  e)
      {
          e.printStackTrace();
          return -1;  
      }
      catch(SQLException e)
      {
          e.printStackTrace();
          return -1;
      }
      catch(Exception e)
      {
          e.printStackTrace();
          return -1;
      }
      finally
      {
          try 
          {
              result.close();
              pstmt.close();
              conn.close();
          }
          catch(SQLException e)
          {
              e.printStackTrace();
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
      }
  }
   
}

servlet层(封装处理从页面传来的值):

package servlet;

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 bean.LoginBean;
import dao.LoginDao;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet 
{
    private static final long serialVersionUID = 1L;

    public LoginServlet() 
    {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException
    {
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("name");
        String pwd=request.getParameter("pwd");
        LoginBean loginbean=new LoginBean(name,pwd);   //将数据传入LoginBean中
        int result=LoginDao.Connect(loginbean);
        if(result>0)     //登录成功
        {
            response.sendRedirect("success.jsp");
        }else if(result==0)   //登录失败(用户名,密码错误)
        {
            request.getRequestDispatcher("fault.jsp").forward(request, response);
        }else      //登录失败(系统错误)
        {
            response.sendRedirect("error.html");
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException 
    {
        doGet(request, response);
    }

}

web页面:

1,登录页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录Insert title here</title>
</head>
<body>
     <h2>登录</h2>
    <form action="LoginServlet" method="post">
       用户名:<input type="text" name="name">
       密  码:<input type="password" name="pwd"> 
       <input type="submit" value="提交">
    </form>
</body>
</html>

2,登录成功,登录失败,系统异常页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
            登录成功
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
     用户名或密码错误
</body>
</html>



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    系统异常
</body>
</html>
原文地址:https://www.cnblogs.com/lq13035130506/p/11679623.html