目标简述:用户在login.jsp页面输入用户名和密码,LoginServlet拿到数据后,调用LoginDao去和数据库中的数据进行校验,LoginServlet再根据LoginDao的返回结果,决定下一步是重新登录还是进入欢迎界面(Welcome.jsp).
原理图:
5个文件:
JSP: login.jsp 和welcome.jsp
Entity:login.java
Servlet:LoginServlet.java
Dao:LoginDao.java
1.login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录界面</title>
</head>
<body>
<form action="LoginServlet" method="get">
用户名:<input type="text" name="uname"> <br/>
密码:<input type ="password" name="upwd"><br/>
<input type="submit" value="登录"><br/>
</form>
</body>
</html>
2.welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录成功欢迎页!</title>
</head>
<body>
登录成功!
</body>
</html>
3.Login.java //用户封装数据的javabean
package org.prolific.entity;
//Javabean 封装数据的模型-实体类
public class Login {
private int id;
private String uname;
private String upwd;
public Login() {
}
public Login(String uname, String upwd) {
this.uname = uname;
this.upwd = upwd;
}
public Login(int id, String uname, String upwd) {
this.id = id;
this.uname = uname;
this.upwd = upwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
4.LoginServlet.java
package org.prolific.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.prolific.dao.LoginDao;
import org.prolific.entity.Login;
//控制层(Controller):接受view请求,并分发给Model处理
public class LoginServlet extends HttpServlet {
public LoginServlet() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理登录
request.setCharacterEncoding("utf-8");
String name= request.getParameter("uname");
String pwd= request.getParameter("upwd");
Login login=new Login(name,pwd); //用户名密码
//调用模型层的 登录功能
int result =LoginDao.login(login);
if (result>0) { //登录成功
response.sendRedirect("welcome.jsp");
}else if(result==0) { //用户名密码有误 (重新登录)
response.sendRedirect("login.jsp");
System.out.println("用户名密码有误!");
}else { //系统异常 (重新登录)
response.sendRedirect("login.jsp");
System.out.println("系统异常!");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("系统异常!");
}
}
5.LoginDao.java //用户封装逻辑的javabean
package org.prolific.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.prolific.entity.*;
//模型层:用于处理登录(查询数据库)
public class LoginDao{
public static int login(Login login) {//登录
int result=-1;
Connection connection=null;
PreparedStatement pstmt =null;
ResultSet rs=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/lds?serverTimezone=UTC","root","prolific");
String sql="select count(*) from login where uname=? and upwd=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1,login.getUname());
pstmt.setString(2,login.getUpwd());
rs= pstmt.executeQuery();
if(rs.next()) {
result=rs.getInt(1);
}
if (result>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 {
if (rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if (connection!=null) connection.close();
}catch(SQLException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}