目录结构
将同一个业务的后端程序整合到一个类中,也是MVC 分层,的雏形
分类 整合
处理类爆炸问题
package com.yanyu;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import resources.DBUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @Author yanyu666_508200729@qq.com
* @Date 2024/11/26 21:53
* @description:解决类爆炸问题
* 1.模拟 HttpServlet 的模板设计思想
* 2.注解式开发
* 3. 在原项目 stuid 基础 上开发,并修改号对应的 项目名字
*/
@WebServlet({"/list","/modifyStuno","/modify","/delete","/add"})
public class StudentService extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取 servlet 对象的路径 /web2/list web2 项目名字, /list
String servletPath = request.getServletPath();// /list
if ("/list".equals(servletPath)) {
// 利用已知道的常量来调研方法,避开空引用异常
// 将 浏览器的请求对象带入
doList(request, response);
} else if ("/add".equals(servletPath)) {
doAdd(request,response);// alt enter
}else if ("/delete".equals(servletPath)){
doDel(request,response);// 不要设置为 doDelete() 会与 HttpServlet 的 doDelete()
} else if ("/modifyStuno".equals(servletPath)) {
doModifyStuno(request,response);
} else if ("/modify".equals(servletPath)) {
doModify(request,response);
}
}
private void doModify(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String stuid = request.getParameter("stuid");
String zhuanye = request.getParameter("zhuanye");
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
connection = DBUtil.getConnection();
connection.setAutoCommit(false);
String sql = "update student set name = '"+name+"',zhuanye = '"+zhuanye+"' where stuid = '"+stuid+"'";
statement = connection.createStatement();
statement.executeUpdate(sql);
connection.commit();
} catch (SQLException e) {
if (connection != null) {
try {
connection.close();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}finally {
DBUtil.close(rs,statement,connection);
}
response.sendRedirect("/web2/list");
}
private void doModifyStuno(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// 获取待续该学生的学号
String stuno = request.getParameter("stuno");
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
connection = DBUtil.getConnection();
connection.setAutoCommit(false);
// 获取 该学生的信息
String sql = "select * from student where stuid = '" + stuno + "'";
statement = connection.createStatement();
rs = statement.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
// rs.getString("stuid");
String zhuanye = rs.getString("zhuanye");
out.print("<!DOCTYPE html>");
out.print("<html lang='en'>");
out.print("<head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
out.print(" <title>Document</title>");
out.print("</head>");
out.print("<body>");
out.print(" <h1>修改学生</h1>");
// 设置 修改后的学生信息
out.print(" <form action='/web2/modify'>");
out.print(" 原姓名:"+name+"");
out.print(" <br>");
out.print(" 原学号:"+stuno+"");
out.print(" <br>");
out.print(" 原专业:"+zhuanye+"");
out.print(" <br>");
out.print(" 姓名:<input type='text' name='name'>");
out.print(" <br>");
// 修改前后,学号是不变的,所以要把学号拼接到 学号输入框 ,并将输入框设置为 只读不可修改
out.print(" 学号:<input type='text' name='stuid' value='"+stuno+"' readonly>");
out.print(" <br>");
out.print(" 专业:<input type='text' name='zhuanye' >");
out.print(" <br><input type='submit' value='确认修改'>");
out.print(" </form>");
out.print("</body>");
out.print("</html>");
}
connection.commit();
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}finally {
DBUtil.close(rs,statement,connection);
}
}
private void doDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// 获取待续该学生的学号
String delno = request.getParameter("delno");
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
connection = DBUtil.getConnection();
connection.setAutoCommit(false);
String sql = "delete from student where stuid = '"+delno+"'";
statement = connection.createStatement();
statement.execute(sql);
connection.commit();
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}finally {
DBUtil.close(rs,statement,connection);
}
response.sendRedirect("/web2/list");
}
private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String stuid = request.getParameter("stuid");
String zhuanye = request.getParameter("zhuanye");
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
connection = DBUtil.getConnection();
connection.setAutoCommit(false);
String sql = "insert into student(name,stuid,zhuanye) values('"+name+"','"+stuid+"','"+zhuanye+"') ";
statement = connection.createStatement();
statement.execute(sql);
connection.commit();
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}finally {
DBUtil.close(rs,statement,connection);
}
response.sendRedirect("/web2/list");
}
private void doList(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 处理整个查询业务 IO异常交给tomcat 处理
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
connection = DBUtil.getConnection();
connection.setAutoCommit(false);
String sql = "select * from student";
statement = connection.createStatement();
rs = statement.executeQuery(sql);
out.print("<!DOCTYPE html>");
out.print("<html lang='en'>");
out.print("<head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
out.print(" <title>Document</title>");
out.print("</head>");
out.print("<body>");
out.print(" <h1>学生信息</h1>");
out.print(" <table border='1px' width='600px'>");
out.print(" <tr>");
out.print(" <td>姓名</td>");
out.print(" <td>学号</td>");
out.print(" <td>专业</td>");
out.print(" <td>操作</td>");
out.print(" </tr>");
while (rs.next()) {
String name = rs.getString("name");
String stuid = rs.getString("stuid");
String zhuanye = rs.getString("zhuanye");
out.print(" <tr></tr>");
out.print(" <td>" + name + "</td>");
out.print(" <td>" + stuid + "</td>");
out.print(" <td>" + zhuanye + "</td>");
out.print(" <td>");
out.print(" <a href='./add.html'>新增</a>");
// 发送修改学生信息之前 ,查询学生原来的信息 根据 stuid 在修改前后不变的原则
out.print(" <a href='/web2/modifyStuno?stuno=" + stuid + "'>修改</a>");
// 根据 stuid 去限制 where 进行学生的 删除操作
out.print(" <a href='/web2/delete?delno=" + stuid + "'>删除</a>");
out.print(" </td>");
out.print(" </tr>");
}
out.print(" </table>");
out.print("</body>");
out.print("</html>");
connection.commit();
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
} finally {
DBUtil.close(rs, statement, connection);
}
}
}
功能测试
其它功能正常