首先来说下主要思路(登录就是判断账号及密码是否在数据库中,进行查操作)
简便来说,从最下层看,数据访问层进行数据库的直接查询操作,业务逻辑层调用数据访问层的方法操作 作逻辑判断,将其逻辑值返回给表示层,由此逻辑值判断登录成功与否,由servlet输出结果
再从最上层说起
1、login.jsp发送请求,要传值给LoginUserServlet.java,在此通过Form表单传值方式,首先将数据封装成JavaBean(实体类)再进行传值。即用User.java进行封装(往下转发时数据都以实体类传)
2、LoginUserServlet.java此时收到请求,并将请求转发给UserService.java
3、业务逻辑层中UserService.java此时进行判断数据库 查
的逻辑性,要调用在数据访问层UserDao.java中的方法,这就需要将请求继续转发给数据访问层
4、在数据访问层中,直接进行数据库的 查 操作
接下来具体实现
login.jsp表单
<form action="LoginUserServlet" method="post">
<input type="text" name="name" ><br>
<input type="password" name="pwd" ><br>
<input type="submit" value="登录">
</form>
User.java
public class User {
private String name;
private String pwd;
public User() {
}
public User(String name, String pwd) {
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;
}
}
LoginUserServlet.java(servlet文件)
import java.io.*;
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 com.starry.entity.User;
import com.starry.service.UserService;
@WebServlet("/LoginUserServlet")
public class LoginUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginUserServlet() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
User user=new User(name,pwd);
UserService userService=new UserService();
boolean flag=userService.LoginUser(user);
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("utf-8");
PrintWriter out=response.getWriter();//响应编码
if(flag) {
out.println("登录成功");
}else{
out.println("重新登录");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
UserService.java
public class UserService {
UserDao userDao=new UserDao();
public boolean LoginUser(User user) {
boolean flag=false;
if(userDao.isExist(user)) {//查询用户是否存在于数据库中
flag=true;
}else {
flag=false;
}
return flag;
}
}
UserDao.java
import java.sql.*;
//数据访问层 增删该查
public class UserDao {
private final String URL = "jdbc:mysql://127.0.0.1/blog?serverTimezone=UTC";//不加serverTime时区就报错
private final String USER="root";
private final String PASSWORD="";
public boolean isExist(User user) {
return queryUser(user)==null?false:true;
//查找此人,返回值为空,代表不存在
}
//根据 名字 和密码 查某人 返回值是User对象,里面有某人的信息
public User queryUser(User user) {//根据用户名查看用户是否存在
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;//建立结果集对象
try {
// 1 加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
String sql="select * from user where name=? and pwd=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getPwd());
rs=pstmt.executeQuery();//返回就一个数据
if(!rs.next()) {
user=null;
}
}catch (ClassNotFoundException e) {
// TODO: handle exception
}catch (SQLException e) {
// TODO: handle exception
}finally {
try {//关闭
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
}catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
return user;
}
}
注册思路和登录相同
1、新建一个regiter.jsp注册页面
<form action="RegiterUserServlet" method="post">
<input type="text" name="name" ><br>
<input type="password" name="pwd" ><br>
<input type="submit" value="注册">
</form>
2、RegiterUserServlet.java(servlet文件)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/RegiterUserServlet")
public class RegiterUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public RegiterUserServlet() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
User user=new User(name,pwd);
UserService userService=new UserService();
boolean flag=userService.RegiterUser(user);
//PrintWriter out=response.getWriter();
//设置响应编码
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("utf-8");
PrintWriter out=response.getWriter();//响应编码
if(flag) {
out.println("注册成功");
}else{
out.println("注册失败(可能此用户已注册)");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
3、在UserService.java增加以下部分
public boolean RegiterUser(User user) {
boolean flag=false;
if(!userDao.isExist(user.getName())) {//不存在,增加
userDao.RegiterUser(user);
flag=true;
}else {
System.out.println("此用户已注册");
flag=false;
}
return flag;
}
4、在UserDao.java中增加一下部分
//判断此人是否存在
public boolean isExist(String name) {
return queryUser(name)==null?false:true;
//查找此人,返回为空,不存在
}
public boolean RegiterUser(User user) {//注册信息
boolean flag=false;
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 1 加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
String sql="insert into user(name,pwd) values(?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getPwd());
int count=pstmt.executeUpdate();
if(count>0) {
flag=true;
}else {
flag=false;
}
}catch (ClassNotFoundException e) {
// TODO: handle exception
flag=false;
}catch (SQLException e) {
// TODO: handle exception
flag=false;
}finally {
try {//关闭
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
}catch (SQLException e) {
//e.printStackTrace();
flag=false;
}catch (Exception e) {
//e.printStackTrace();
flag=false;
}
}
return flag;
}
//根据 名字查某人 返回值是User对象,里面有某人的信息
public User queryUser(String name) {//根据用户名查看用户是否存在
User user=null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;//建立结果集对象
try {
// 1 加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
String sql="select * from user where name=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, name);
rs=pstmt.executeQuery();//返回就一个数据
if(rs.next()) {
String pwd=rs.getString("pwd");
user=new User(name,pwd);
}
}catch (ClassNotFoundException e) {
// TODO: handle exception
}catch (SQLException e) {
// TODO: handle exception
}finally {
try {//关闭
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
}catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
return user;
}