JavaWeb学习大纲传送门
兄弟姊妹目录
- 页外目录链接:
-------------------------------<注册篇>-------------------------------
-------------------------------<作用域篇>-------------------------------
-------------------------------<数据展示篇>-------------------------------
本章学习目录
一,login.jsp(登陆页面的设置)
-
浏览器显示:
-
login.jsp代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆界面</title>
</head>
<body>
<h2>登录界面</h2>
<form action="login.do">
账户:<input type="text" name="user_name">
密码:<input type="password" name="user_password">
<button >登录</button>
<button type="reset">重置</button>
</form>
</body>
</html>
二,servlet层(获取登陆页面操作层)
- LoginServlet代码:
package com.jazhong.servlet;
import com.jazhong.service.UserService;
import com.jazhong.service.impl.UserServiceImpl;
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 java.io.IOException;
@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码集
request.setCharacterEncoding("UTF-8");
//获得浏览器的输入
String user_name=request.getParameter("user_name");
String user_password=request.getParameter("user_password");
//调用dervice层,逆向建方法
UserService userService=new UserServiceImpl();
//因为userService.login返回的是boolean值,所以直接看是否为true是就跳入index.jsp页面,不然跳入register.jsp页面
if (userService.login(user_name,user_password)==true){
response.sendRedirect("/index.jsp");
}else {
response.sendRedirect("/register.jsp");
}
}
}
三,service层(服务层)(包含了之前register的方法,因为它们都是在UserService接口和UserServiceImpl类里)
- UserServiceImpl的login方法(只看login,register是整个大流程里注册的方法)
package com.jazhong.service.impl;
import com.jazhong.dao.UserDao;
import com.jazhong.dao.impl.UserDaoImpl;
import com.jazhong.model.User;
import com.jazhong.service.UserService;
import java.util.List;
public class UserServiceImpl implements UserService {
@Override
public void register(User user) {
//逆向建类的方式建立com.jazhong.dao包和包下的UserDao,
UserDao userDao = new UserDaoImpl();
userDao.addUser(user);
}
@Override
public boolean login(String user_name, String user_password) {
UserDao userDao = new UserDaoImpl();
//由于myexecuteQuerylist方法返回List<User>所以用它接收
User user = userDao.QueryByName(user_name);
if (user != null && user.getUser_password().equals(user_password)) {
return true;
} else {
return false;
}
}
}
四,dao层(数据库操作层)(包含了之前register的方法,因为它们都是在UserDao接口和UserDaoImpl类里)
- DBUtil工具包
package com.jazhong.model;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DBUtil {
protected Connection conn=null;
protected PreparedStatement ps=null;
protected ResultSet rs=null;
//获得连接
protected Connection getconn() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/goods?useSSL=true","root","root");
return conn;
}
//资源释放
protected void closeall(){
try {
if (rs!=null){rs.close();}
if (ps!=null){ps.close();}
if (conn!=null){conn.close();}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
rs=null;
ps=null;
conn=null;
}
}
/**
* 考虑到sql和?占位符params的变化性(小编前面文章有)
* 该方法适于数据库的增,删,改
* @param sql 增删改语句
* @param params 设置?占位符 ...表示可变长的参数
*/
protected void myexecuteUpdate(String sql,Object... params){
try {
//调用获取连接方法
this.getconn();
ps=conn.prepareStatement(sql);
//设置?占位符params
if (sql!=null&¶ms.length!=0){
for (int i=0;i<params.length;i++){
ps.setObject(i+1,params[i]);
}
}
//判断是否操作成功
if (ps.executeUpdate()>0){
System.out.println("操作成功");
}else {
System.out.println("操作失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
this.closeall();
}
}
/**
* 根据某个信息查询这个人的所有信息
*/
protected User myexecuteQuerylist(String sql, Object... params){
//提升作用域
User user=null;
try {
this.getconn();
ps=conn.prepareStatement(sql);
if (sql!=null&¶ms.length!=0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
rs=ps.executeQuery();
if (rs.next()){
user=new User();
user.setUser_id(rs.getInt(1));
user.setUser_name(rs.getString(2));
user.setUser_password(rs.getString(3));
user.setUser_sex(rs.getInt(4));
user.setUser_hobbys(rs.getString(5));
user.setUser_birthday(rs.getDate(6));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeall();
}
return user;
}
}
- UserDaoImpl的QueryByName方法(只看login,register是整个大流程里注册的方法)
package com.jazhong.dao.impl;
import com.jazhong.dao.UserDao;
import com.jazhong.model.DBUtil;
import com.jazhong.model.User;
import java.util.List;
public class UserDaoImpl extends DBUtil implements UserDao {
@Override
public void addUser(User user) {
//定义有关注册的SQL语句
String sql="insert into tbl_user values(default,?,?,?,?,?)";
//调用DBUtil中的myexecuteUpdate方法,对数据库进行操作这里看不懂回去复习一下JDBC
super.myexecuteUpdate(sql,user.getUser_name(),user.getUser_password(),
user.getUser_sex(),user.getUser_hobbys(),user.getUser_birthday());
}
@Override
public User QueryByName(String user_name) {
//定义有关登陆的sql语句
String sql="select * from tbl_user where user_name=?";
//由于myexecuteQuerylist方法返回List<User>所以用它接收
User user=super.myexecuteQuerylist(sql,user_name);
return user;
}
}
五,登录测试
- 已有用户
- 进入网页测试登录
- 登陆成功,进入index.jsp页面
- 登陆失败,进入register.jsp页面