index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登陆页面</title>
<style>
.title {
text-align: center;
}
.error {
text-align: center;
color: red;
font-weight: bold;
}
#loginForm {
width: 300px;
border: 1px solid #666;
margin: 50px auto;
padding: 50px;
}
.button {
text-align: center;
padding: 10px;
}
.input {
text-align: center;
padding: 10px;
}
</style>
<body>
<h2 class="title">登陆页面</h2>
<%--El表达式可以从request,session,application对象中自动续找对应的属性值--%>
<div class="error">${msg}</div>
<form action="LoginServlet" method="post" id="loginForm">
<div class="input">
<span>学号: </span><input name="stuno" type="text" placeholder="请输入学号">
</div>
<div class="input">
<span>密码: </span><input name="password" type="password"
placeholder="请输入密码">
</div>
<div class="button">
<input type="submit" value="登录">
</div>
</form>
</body>
</html>
result.jsp
<%@ 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>
欢迎您,${stuname}
</body>
</html>
UserDao.java
package dao;
//定义接口
public interface UserDao {
//用来获取用户信息,与数据库进行匹配
String getUser(String stuno, String password);
}
UserDaoImpl.java
package dao.impl;
import java.sql.*;
import dao.UserDao;
import util.DatabaseBean;
//实现用户访问操作的接口
public class UserDaoImpl implements UserDao{
//重写接口下面的方法,用于查询数据库进行比对
public String getUser(String stuno, String password) {
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
String stuname =null;
String sql = "select stuname from user where stuno = ? and password = ?";
try {
conn = DatabaseBean.getConnection();
psmt = conn.prepareStatement(sql);
psmt.setString(1, stuno);
psmt.setString(2, password);
rs = psmt.executeQuery();
//找到记录进行判断,找到则返回学生姓名
if(rs.next()) {
stuname = rs.getString("stuname");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {//不断程序正常还是错误,都会进行finally里面的代码
//关闭代码的运用
DatabaseBean.close(rs, psmt, conn);
}
return stuname;
}
}
User.java
package model;
public class User {
private String stuno;
private String stuname;
private String password;
public User() {
}
public User(String stuno, String stuname, String password) {
this.stuno = stuno;
this.stuname = stuname;
this.password = password;
}
public String getStuno() {
return stuno;
}
public void setStuno(String stuno) {
this.stuno = stuno;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
LoginServlet.java
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 javax.servlet.http.HttpSession;
import util.DaoFactory;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String stuno = request.getParameter("stuno");
String password = request.getParameter("password");
String stuname = DaoFactory.getUserDaoImpl().getUser(stuno, password);
if(stuname!=null) {
//登陆成功
request.setAttribute("stuname", stuname);
request.getSession().setAttribute("msg","");
//页面跳转 第一种方式 转发
request.getRequestDispatcher("result.jsp").forward(request, response);
//第二种方式 重定向 不能携带数据
//response.sendRedirect("index.jsp");
}else {
//登陆失败
HttpSession session = request.getSession();
request.getSession().setAttribute("msg", "你的学号或者密码错误,请你重新输入");
response.sendRedirect("index.jsp");
}
}
}
TsetUser.java
package test;
import util.DaoFactory;
import dao.UserDao;
import dao.impl.UserDaoImpl;
public class TestUser {
//进行测试
public static void main(String[] args) {
String stuname = DaoFactory.getUserDaoImpl().getUser("202202373", "123");
if(stuname!=null) {
System.out.println(stuname + "登陆成功");
}else {
System.out.println("登陆失败");
}
}
}
DaoFactory.java
package util;
import dao.UserDao;
import dao.impl.UserDaoImpl;
//使测试代码更加简洁
public class DaoFactory {
public static UserDao getUserDaoImpl() {
return new UserDaoImpl();
}
}
DatabaseBean.java
package util;
import java.sql.*;
public class DatabaseBean {
//避免重复地获取数据库的连接
public static Connection getConnection() {
Connection conn = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, "root", "123456");
} catch (ClassNotFoundException e) {
// 将可能发生异常的代码包裹起来,java代码需要使用这些方法来进行异常捕获
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//直接进行关闭数据库的操作
public static void close(ResultSet rs, Statement stmt, Connection conn) {
try {
// 判空处理,同样需要包裹起来
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}