DeleteServlet:
package com.zh.servlet; import java.io.IOException; 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.zh.service.*; @WebServlet("/DeleteServlet") public class DeleteServlet extends HttpServlet { protected void service(HttpServletRequest arg0,HttpServletResponse arg1) throws ServletException ,IOException{ arg1.setContentType("text/html"); arg0.setCharacterEncoding("GBK"); arg1.setCharacterEncoding("GBK"); String name=arg0.getParameter("teacher_name"); Userservice service =new Userservice(); service.deleteUser(name); try { arg0.getRequestDispatcher("/query").forward(arg0, arg1); }catch (Exception e) { e.printStackTrace(); } } }
InsertServlet:
package com.zh.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; import com.zh.service.*; @WebServlet("/InsertServlet") public class InsertServlet extends HttpServlet { protected void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); req.setCharacterEncoding("GBK"); res.setCharacterEncoding("GBK"); String place=req.getParameter("place"); String teacher_name=req.getParameter("teacher_name"); String class_name=req.getParameter("class_name"); PrintWriter pw=res.getWriter(); Userservice service=new Userservice(); try { service.insertUser(place, teacher_name, class_name); pw.println("新增信息成功!"); req.getRequestDispatcher("/query").forward(req, res);; }catch (Exception e) { e.printStackTrace(); req.getRequestDispatcher("/insert.html").forward(req, res); } } }
QueryServlet:
package com.zh.servlet; import java.io.IOException; 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.PrintWriter; import com.zh.entity.*; import com.zh.service.*; import java.util.*; @WebServlet("/QueryServlet") public class QueryServlet extends HttpServlet { protected void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); req.setCharacterEncoding("GBK"); res.setCharacterEncoding("GBK"); PrintWriter pw=res.getWriter(); Userservice service=new Userservice(); List list=service.findAll_user(); pw.println("<html>"); pw.println("<head>"); pw.println("</head>"); pw.println("<body>"); pw.println("<table border='1' bordercolor='red' align='center'"); pw.println("<tr>"); pw.println("<td>"); pw.println("地点"); pw.println("</td>"); pw.println("<td>"); pw.println("教师名"); pw.println("</td>"); pw.println("<td>"); pw.println("课程名"); pw.println("</td>"); pw.println("</tr>"); for(int i=0;i<list.size();i++) { bean b=(bean) list.get(i); pw.println("<tr>"); pw.println("<td>"); pw.println(b.get_place()); pw.println("</td>"); pw.println("<td>"); pw.println(b.get_teacher_name()); pw.println("</td>"); pw.println("<td>"); pw.println(b.get_class_name()); pw.println("/<td>"); pw.println("<a href='/Web_operation1/delete?teacher_name="+b.get_teacher_name()+"'> 删 除 </a>||<a href='/Web_operation1/updata.html'>修改信息</a>" ); pw.println("</td>"); pw.println("</tr>"); } pw.println("</table>"); pw.println("</body>"); pw.println("</html>"); pw.flush(); pw.close(); } }
UpdateServlet:
package com.zh.servlet; import java.io.IOException; 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.PrintWriter; import com.zh.service.*; @WebServlet("/UpdataServlet") public class UpdateServlet extends HttpServlet { protected void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); req.setCharacterEncoding("GBK"); res.setCharacterEncoding("GBK"); PrintWriter pw=res.getWriter(); Userservice service=new Userservice(); String place=req.getParameter("place"); String teacher_name=req.getParameter("teacher_name"); String class_name=req.getParameter("class_name"); try { if (service.updataByteacher_name(place, class_name, teacher_name)) { res.sendRedirect("/Web_operation1/query"); } }catch (Exception e) { e.printStackTrace(); } } }
insert.html:
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta charset="UTF-8"> <title>Insert </title> </head> <body> <center> <form action="/Web_operation1/insert"medthod="post"> <table border ="1" bordercolor="red"align="center"> <tr> <td> 上课地点: </td> <td> <input type="text" name="place"/> </td> </tr> <tr> <td> 任课教师: </td> <td> <input type="text" name="teacher_name"/> </td> </tr> <tr> <td> 课程名称: </td> <td> <input type="text" name="class_name"/>; </td> </tr> </table> </form> </center> </body> </html>
update.html:
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta charset="UTF-8"> <title>update</title> </head> <body> <center> <form action="/Web_operation1/update" method="post"> <table border="1"bordercolor="red" align="center"> <tr> <td> 上课地点:</td> <td> <input type="text" name="place"> </td> </tr> <tr> <td> 任课教师:</td> <td> <input type="text" name="teacher_name"> </td> </tr> <tr> <td> 课程名称:</td> <td> <input type="text"name="class_name"> </td> </tr> <tr> <td colspan="2"align="center"> <input type="button" value="确认"> </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://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <display-name>Web_operation1</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>query</servlet-name> <servlet-class>com.zh.servlet.QueryServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>query</servlet-name> <url-pattern>/query</url-pattern> </servlet-mapping> <servlet> <servlet-name>delete</servlet-name> <servlet-class>com.zh.servlet.DeleteServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>delete</servlet-name> <url-pattern>/delete</url-pattern> </servlet-mapping> <servlet> <servlet-name>update</servlet-name> <servlet-class>com.zh.servlet.UpdateServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>update</servlet-name> <url-pattern>/update</url-pattern> </servlet-mapping> <servlet> <servlet-name>insert</servlet-name> <servlet-class>com.zh.servlet.InsertServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>insert</servlet-name> <url-pattern>/insert</url-pattern> </servlet-mapping> </web-app>