CategoryServlet,正如第4篇中提到的这个Servlet主要是针对Category数据的操作,包括添加,删除,修改,更新等等。
之所以拿CategoryServlet纯粹因为它比较简单。
对于CategoryServlet的基本思路是:接收requst传来的参数method,然后在根据method的要求来进行不同的操作。
1 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
2 throws ServletException, IOException {
3 response.setContentType("text/html;charset=UTF-8");
4 request.setCharacterEncoding("UTF-8");
5 String method = request.getParameter("method");
6 if (method.equals("add")) {
7 add(request, response);
8 } else if (method.equals("delete")) {
9 delete(request, response);
10 } else if (method.equals("edit")) {
11 preEdit(request, response);
12 } else if (method.equals("update")) {
13 update(request, response);
14 } else if (method.equals("list")) {
15 list(request, response);
16 }
17 }
直接添加这些方法,netbeans会有无法找到对应方法的提示,
点击黄色小灯泡,IDE自动生成对应的方法,建议在这些方法后面添加throws语句。例:
1 private void add(HttpServletRequest request, HttpServletResponse response)
2 throws ServletException, IOException {
3
4 }
a. add方法:接收分类名称(category.name),QueryRunner对象根据sql命令执行update操作,request传回结果(比如:转到对应页面)
1 private void add(HttpServletRequest request, HttpServletResponse response)
2 throws ServletException, IOException {
3 String name = request.getParameter("name");
4 String sql = "insert into category (name) values (?)";
5 String params[] = {name};
6 QueryRunner qr = DbHelper.getQueryRunner();
7 try {
8 qr.update(sql, params);
9 } catch (SQLException ex) {
10 Logger.getLogger(CategoryServlet.class.getName()).log(Level.SEVERE, null, ex);
11 }
12 list(request,response);
13 }
b. delete方法:接收分类类别(category.id),QueryRunner对象根据sql语句执行update操作,request传回结果(比如:转到对应页面)
1 private void delete(HttpServletRequest request, HttpServletResponse response)
2 throws ServletException, IOException {
3 String id = request.getParameter("id");
4 String sql = "delete from category where id = "+id;
5
6 QueryRunner qr = DbHelper.getQueryRunner();
7 try {
8 qr.update(sql);
9 } catch (SQLException ex) {
10 Logger.getLogger(CategoryServlet.class.getName()).log(Level.SEVERE, null, ex);
11 }
12
13 list(request,response);
14 }
c. preEdit方法:接收分类类别(category.id),QueryRunner对象根据sql语句执行query操作,request传回结果,转入对应编辑页面;
1 private void preEdit(HttpServletRequest request, HttpServletResponse response)
2 throws ServletException, IOException {
3 String id = request.getParameter("id");
4 String sql = "select id,name from category where id = "+id;
5 List categories = null;
6 Category category = null;
7
8 QueryRunner qr = DbHelper.getQueryRunner();
9 try {
10 categories = (List) qr.query(sql, new BeanListHandler(Category.class));
11 } catch (SQLException ex) {
12 Logger.getLogger(CategoryServlet.class.getName()).log(Level.SEVERE, null, ex);
13 }
14
15 if (categories != null){
16 category = (Category)categories.get(0);
17 }
18 request.setAttribute("category", category);
19 request.getRequestDispatcher("/admin/editCategory.jsp").forward(request, response);
20 }
d. update方法:接收分类名称(name),编号(id),QueryRunner对象根据sql语句执行update操作,request传回结果(比如:转到对应页面);
1 private void update(HttpServletRequest request, HttpServletResponse response)
2 throws ServletException, IOException {
3 String id = request.getParameter("id");
4 String name = request.getParameter("name");
5 String sql = "update category set name = ? where id = ?";
6 String params[] = {name,id};
7
8 QueryRunner qr = DbHelper.getQueryRunner();
9 try {
10 qr.update(sql, params);
11 } catch (SQLException ex) {
12 Logger.getLogger(CategoryServlet.class.getName()).log(Level.SEVERE, null, ex);
13 }
14 list(request,response);
15 }
e. list方法:list所有相关的分类,request传回结果(比如:转到对应页面);
1 private void list(HttpServletRequest request, HttpServletResponse response)
2 throws ServletException, IOException {
3 String sql = "select id, name from category order by name";
4 List categories = null;
5
6 QueryRunner qr = DbHelper.getQueryRunner();
7 try {
8 categories = (List) qr.query(sql, new BeanListHandler(Category.class));
9 } catch (SQLException ex) {
10 Logger.getLogger(CategoryServlet.class.getName()).log(Level.SEVERE, null, ex);
11 }
12
13 request.setAttribute("categories", categories);
14 request.getRequestDispatcher("/admin/adminCategoryList.jsp").forward(request, response);
15 }