【JAVA】Servlet有条理的实现层次分明的注册及注销
前言
Java项目实习第四次课后作业:实现Tomcat简单注册及注销界面。
前期准备
导入jar包
设置数据库
Code
model下的User类
package com.zr0629.model;
public class User {
private int id;
private String name;
private String password;
private String age;
//按住键盘上的alt + insert键
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", age='" + age + '\'' +
'}';
}
}
util下的DBUtil类
package com.zr0629.util;
import java.sql.*;
public class DBUtil {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("连接数据库");
//创建连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/testtt?useSSL=false&serverTimezone=Asia/Shanghai","root","123456");
return connection;
}
public static void closeAll(ResultSet rs, Statement stmt,Connection conn) throws SQLException {
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
}
dao下的UserDao类
package com.zr0629.dao;
import com.zr0629.model.User;
import com.zr0629.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.stream.Stream;
public class UserDao {
public int add(User user){
Connection connection = null;
PreparedStatement pstmt = null;
int count = 0;
try{
connection = DBUtil.getConnection();
System.out.println("连接成功");
//获取执行sql的Statement对象
pstmt = connection.prepareStatement("insert into user(name,password,age) values (?,?,?)");
pstmt.setString(1,user.getName());
pstmt.setString(2,user.getPassword());
pstmt.setString(3,user.getAge());
//执行sql,获取结果
count = pstmt.executeUpdate();
System.out.println("insert操作:" + count);
return count;
}catch (Exception e){
e.printStackTrace();
}finally {
try {
DBUtil.closeAll(null,pstmt,connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
return count;
}
public int delete(User user){
Connection connection = null;
PreparedStatement pstmt = null;
int count = 0;
try{
connection = DBUtil.getConnection();
//获取执行sql的Statement对象
pstmt = connection.prepareStatement("delete from user where name = ? and password = ? and age = ?");
pstmt.setString(1,user.getName());
pstmt.setString(2,user.getPassword());
pstmt.setString(3,user.getAge());
//执行sql,获取结果
count = pstmt.executeUpdate();
System.out.println("delete操作:" + count);
return count;
}catch (Exception e){
e.printStackTrace();
}finally {
try {
DBUtil.closeAll(null,pstmt,connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
return count;
}
}
service下的UserService类
package com.zr0629.service;
import com.zr0629.dao.UserDao;
import com.zr0629.model.User;
public class UserService {
UserDao userDao = new UserDao();
public int add(User user){
System.out.println("service中add方法被调用");
return userDao.add(user);
}
public int delete(User user){
System.out.println("service中del方法被调用");
return userDao.delete(user);
}
}
servlet下的AddServlet类
package com.zr0629.servlet;
import com.zr0629.model.User;
import com.zr0629.service.UserService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class AddServlet extends HttpServlet {
UserService userService = new UserService();
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行了doPost");
String method = req.getParameter("method");
if(method.equals("save")){
insert(req,resp);
}
}
public void insert(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
User user = new User();
String name = req.getParameter("name");
String password = req.getParameter("password");
String age = req.getParameter("age");
user.setName(name);
user.setPassword(password);
user.setAge(age);
System.out.println(user);
int count = userService.add(user);
if(count>0){
resp.sendRedirect("/index.jsp");
}else {
resp.getWriter().write("failed");
}
}
}
servlet下的AddServlet类
package com.zr0629.servlet;
import com.zr0629.model.User;
import com.zr0629.service.UserService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class DeleteServlet extends HttpServlet {
UserService userService = new UserService();
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行了doPost");
String method = req.getParameter("method");
if(method.equals("delete")){
delete(req,resp);
}
}
public void delete(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
User user = new User();
String name = req.getParameter("name");
String password = req.getParameter("password");
String age = req.getParameter("age");
user.setName(name);
user.setPassword(password);
user.setAge(age);
System.out.println(user);
int count = userService.delete(user);
if(count>0){
resp.sendRedirect("/index.jsp");
}else {
resp.getWriter().write("failed");
}
}
}
web下的web.xml页面
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>addservlet</servlet-name> <!--与下方mapping的name保持一致-->
<servlet-class>com.zr0629.servlet.AddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addservlet</servlet-name> <!--与上方name保持一致-->
<url-pattern>/add</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>deleteServlet</servlet-name> <!--与下方mapping的name保持一致-->
<servlet-class>com.zr0629.servlet.DeleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>deleteServlet</servlet-name> <!--与上方name保持一致-->
<url-pattern>/delete</url-pattern>
</servlet-mapping>
</web-app>
web下的index.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1>登陆</h1>
<form action="/login" method="post">
name:<input name="name" type="text">
password:<input name="password" type="password">
<input type="submit" value="login">
</form>
<a href="add.jsp">注册</a>
<a href="delete.jsp">注销</a>
</body>
</html>
web下的add.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<h1>注册</h1>
<body>
<form action="/add?method=save" method="post"> <!-- 对应AddServlet中的if语句-->
name:<input name="name" type="text">
password:<input name="password" type="password">
age:<input name="age" type="text">
<input type="submit" value="注册" >
</form>
</body>
</html>
web下的delete.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注销账户</title>
</head>
<h1>注销</h1>
<body>
<form action="/delete?method=delete" method="post"> <!-- 对应AddServlet中的if语句-->
name:<input name="name" type="text">
password:<input name="password" type="password">
age:<input name="age" type="text">
<input type="submit" value="注销" >
</form>
</body>
</html>
效果图
最开始的user表
运行程序后的开始界面-index,jsp
点击注册超链接
输入未有名称进行注册
注册后的user表
再返回点击注销超链接
输入已有名称进行注销
注销后的user表