项目目录
![项目目录](https://img-blog.csdnimg.cn/20201214194116630.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjIwMzU3NA==,size_16,color_FFFFFF,t_70)
首页index
<%@ 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>
<a href="<%=request.getContextPath()%>/student.do?method=query">查看所有学生信息</a>
</body>
</html>
接口
public interface IStudentService {
void UpdateStudent(String sno, String sname, String sage, String ssex) throws ClassNotFoundException, SQLException;
Map<String, Object> queryForList(String sno) throws ClassNotFoundException, SQLException;
void deleteStudentOne(String sno) throws ClassNotFoundException, SQLException;
List<Map<String, Object>> queryStudentAll() throws ClassNotFoundException, SQLException;
void add(String sname, String sage, String ssex) throws ClassNotFoundException, SQLException;
}
方法类继承了HttpServlet
@SuppressWarnings("serial")
public class StudentServlet extends HttpServlet {
IStudentService StudentService = new StudentServiceImpl();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
System.out.println("StudentServlet-->service()");
String method = request.getParameter("method");
try {
if ("query".equals(method)) {
this.query(request, response);
} else if ("delete".equals(method)) {
this.Delete(request, response);
} else if ("addPage".equals(method)) {
this.addPage(request, response);
} else if ("add".equals(method)) {
this.add(request, response);
} else if ("editPage".equals(method)) {
this.editPage(request, response);
} else if ("edit".equals(method)) {
this.edit(request, response);
}
} catch (ClassNotFoundException e) {
System.out.println("类没有找到异常");
} catch (SQLException e) {
System.out.println("SQL语句异常");
}
}
private void edit(HttpServletRequest request, HttpServletResponse response)
throws ClassNotFoundException, SQLException, IOException {
String sno = request.getParameter("sno");
String sname = request.getParameter("sname");
String sage = request.getParameter("sage");
String ssex = request.getParameter("ssex");
StudentService.UpdateStudent(sno, sname, sage, ssex);
response.sendRedirect(request.getContextPath() + "/student.do?method=query");
}
private void editPage(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ClassNotFoundException, SQLException {
String sno = request.getParameter("sno");
Map<String, Object> map = StudentService.queryForList(sno);
request.setAttribute("map", map);
request.getRequestDispatcher("/jsps/students/edit_student.jsp").forward(request, response);
}
private void add(HttpServletRequest request, HttpServletResponse response)
throws ClassNotFoundException, SQLException, ServletException, IOException {
String sname = request.getParameter("sname");
String sage = request.getParameter("sage");
String ssex = request.getParameter("ssex");
StudentService.add(sname, sage, ssex);
response.sendRedirect(request.getContextPath() + "/student.do?method=query");
}
private void addPage(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/jsps/students/add_student.jsp").forward(request, response);
}
private void Delete(HttpServletRequest request, HttpServletResponse response)
throws ClassNotFoundException, SQLException, IOException {
String sno = request.getParameter("sno");
System.out.println(sno);
StudentService.deleteStudentOne(sno);
response.sendRedirect(request.getContextPath() + "/student.do?method=query");
}
private void query(HttpServletRequest request, HttpServletResponse response)
throws ClassNotFoundException, SQLException, ServletException, IOException {
List<Map<String, Object>> list = StudentService.queryStudentAll();
System.out.println(list);
request.setAttribute("list", list);
request.getRequestDispatcher("/jsps/students/List_Student.jsp").forward(request, response);
}
}
接口实现类
public class StudentServiceImpl implements IStudentService {
Dao dao = new DaoImpl();
public List<Map<String, Object>> queryStudentAll() throws ClassNotFoundException, SQLException {
return dao.executeQueryForList("select * from student");
}
public void deleteStudentOne(String sno) throws ClassNotFoundException, SQLException {
dao.executeUpdate("delete from student where sno='"+sno+"' ");
}
public void add(String sname, String sage, String ssex) throws ClassNotFoundException, SQLException {
dao.executeUpdate(" insert into student values ('"+UUID.randomUUID().toString().substring(0, 8)+"','"+sname+"','"+sage+"','"+ssex+"') ");
}
public Map<String, Object> queryForList(String sno) throws ClassNotFoundException, SQLException {
return dao.executeQueryForMap("select * from student where sno='"+sno+"'");
}
public void UpdateStudent(String sno, String sname, String sage, String ssex) throws ClassNotFoundException, SQLException {
dao.executeUpdate(" update student set sname='"+sname+"',sage='"+sage+"',ssex='"+ssex+"' where sno='"+sno+"' ");
}
jsp Student 首页
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@ 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>
<table border="2px" style="text-align: center; margin: auto">
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>操作</td>
</tr>
<%
List list = (List)request.getAttribute("list");
for(int i = 0; i<list.size(); i++){
Map map = (Map)list.get(i);
%>
<tr>
<td><%=(i+1)%></td>
<td><%=map.get("sname") %></td>
<td><%=map.get("sage") %></td>
<td><%=map.get("ssex") %></td>
<td>
<a href="<%=request.getContextPath()%>/student.do?method=addPage">添加</a>
<a href="<%=request.getContextPath()%>/student.do?method=editPage&sno=<%=map.get("sno")%>">修改</a>
<a href="<%=request.getContextPath()%>/student.do?method=delete&sno=<%=map.get("sno")%>">删除</a>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
jsp Student 添加页面
<%@ 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>
<form action="<%=request.getContextPath()%>/student.do?method=add"
method="post">
<table border="2px" style="text-align: center; margin: auto">
<tr>
<td>姓名</td>
<td><input type="text" name="sname" value="" /></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="sage" value="" /></td>
</tr>
<tr>
<td>性别</td>
<td><input type="text" name="ssex" value="" /></td>
</tr>
<tr>
<td>操作</td>
<td><input type="submit" value="提交" /></a></td>
</tr>
</table>
</form>
</body>
</html>
jsp Student 修改页面
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@ 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>
<%
Map map = (Map) request.getAttribute("map");
%>
<form action="<%=request.getContextPath()%>/student.do?method=edit"
method="post">
<table border="2px" style="text-align: center; margin: auto">
<tr>
<td>姓名</td>
<td><input type="hidden" name="sno" value="<%=map.get("sno")%>" />
<input type="text" name="sname" value="<%=map.get("sname")%>" /></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="sage" value="<%=map.get("sage")%>" /></td>
</tr>
<tr>
<td>性别</td>
<td><input type="text" name="ssex" value="<%=map.get("ssex")%>" /></td>
</tr>
<tr>
<td>操作</td>
<td><input type="submit" value="提交" /></a></td>
</tr>
</table>
</form>
</body>
</html>
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Testservlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>StudentServlet</servlet-name>
<servlet-class>com.rj.bd.student.StudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentServlet</servlet-name>
<url-pattern>/student.do</url-pattern>
</servlet-mapping>
</web-app>