本文主要是介绍Java基础入门day53,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
day53
servlet的升级02
特点
目前项目的设计是,每一个请求都交给一个单独的servlet,AllStudentServlet用来处理查询所有学生的请求,GetStudentByPage用来处理学生分页查询亲够,GetStudentByIdServet用来处理查询单个学生的请求
这样设计的缺点是,对于单个的学生的crud操作,我们都是每一个请求都设计了一个单独的servlet
使得项目设计显得臃肿
能不能将只要是学生的操作,查询所有学生,查询单个学生,分页查询学生,对于学生的增删改操作都交给学生的servlet比如StudentServlet,原来的多个servlet都交给一个servlet中的多个方法处理
我们可以人为地设计一个变量,动态觉得到底使用哪个方法
package com.saas.servlet; import com.saas.entity.Student; import com.saas.service.IStudentService; import com.saas.service.impl.StudentServiceImpl; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.List; @WebServlet(urlPatterns = "/StudentServlet") public class StudentServlet extends HttpServlet { private IStudentService iss = new StudentServiceImpl(); @Overridepublic void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {String op = req.getParameter("op"); if(op == null){getStudentsByPage(req, res);}else if("saveStudent".equalsIgnoreCase(op)){saveStudent(req, res);}else if("deleteStudent".equalsIgnoreCase(op)){deleteStudent(req, res);}} protected void deleteStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("deleteStudent");String ssid = req.getParameter("sid");System.out.println(ssid); int sid = ssid == null ? 0 : Integer.parseInt(ssid); if(iss.deleteStudent(sid)){resp.sendRedirect("StudentServlet");}else{resp.sendRedirect("StudentServlet");}} protected void saveStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=UTF-8"); String ssid = req.getParameter("sid"); int sid = ssid == null ? 0 : Integer.parseInt(ssid); String name = req.getParameter("name");String sex = req.getParameter("sex");String sscore = req.getParameter("score"); double score = sscore == null ? 0 : Double.parseDouble(sscore); String scid = req.getParameter("cid");int cid = scid == null ? 0 : Integer.parseInt(scid);String sage = req.getParameter("age");int age = sage == null ? 0 : Integer.parseInt(sage); Student s = new Student(sid, name, sex, score, cid, age); boolean flag = iss.saveStudent(s); if(flag){resp.sendRedirect("StudentServlet");}else{resp.sendRedirect("stu.html");}} protected void getStudentsByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=UTF-8"); int pageNo = 1;int pigeSize = 50; List<Student> list = iss.getStudentsByPage(pageNo, pigeSize); PrintWriter out = resp.getWriter(); out.print("<table border='1' align='center' width='80%'>");out.print("<tr>");out.print("<th>编号</th>");out.print("<th>姓名</th>");out.print("<th>性别</th>");out.print("<th>分数</th>");out.print("<th>班级</th>");out.print("<th>年龄</th>");out.print("<th>管理</th>");out.print("</tr>");for (Student s : list) {out.print("<tr>");out.print("<td>" + s.getSid() + "</td>");out.print("<td>" + s.getName() + "</td>");out.print("<td>" + s.getSex() + "</td>");out.print("<td>" + s.getScore() + "</td>");out.print("<td>" + s.getCid() + "</td>");out.print("<td>" + s.getAge() +"</td>");out.print("<td><a href='StudentServlet?op=deleteStudent&sid=" + s.getSid() + "'>delete</a></td>");out.print("</tr>");}} }以上代码中的service方式是我们Servlet的声明周期方法,只要调用该servlet,就一定会执行该方法
在该方法中人为地设计一个变量op,可以通过op来动态决定到底调用哪个方法
当前设计中op为空时,我们用来做分页查询
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <h1>this is save student page.</h1> <form action='StudentServlet?op=saveStudent' method='post'>sid:<input type="text" name="sid"><p/>name:<input type="text" name="name"><p/>sex:<input type="text" name="sex"><p/>age:<input type="text" name="age"><p/>score:<input type="text" name="score"><p/>cid:<input type="text" name="cid"><p/><input type="submit" value="save"><p/></form> </body> </html>op为saveStudent时,动态调用当前StudentServlet中的saveStudent方法,在service方法中实现分支处理
StudentServlet?op=deleteStudent当op为deleteStudent时,动态调用当前StudentServlet中的deleteStudent方法,也在service方法中实现分支处理
最终只要是关于Student的所有crud操作都交给了当前的一个StudentServlet,整个项目类设计进行合并,项目结构清爽
这篇关于Java基础入门day53的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!