📄 noteservlet.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.shx.note.servlet;import com.shx.note.factory.DAOFactory;import com.shx.note.vo.Note;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * * @author Administrator */public class NoteServlet extends HttpServlet { // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。"> /** * Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 进行乱码处理 request.setCharacterEncoding("UTF-8"); String path = null; // 声明一个集合类,用于保存错误信息 List errors = new ArrayList(); // 接收要操作的参数值 String status = request.getParameter("status"); if (status != null) { // 参数有内容,之后选择合适的方法 // 查询全部操作 if ("quaryAll".equals(status)) { try { request.setAttribute("all", DAOFactory.getNoteDAOInstance().quaryAll()); } catch (Exception ex) { Logger.getLogger(NoteServlet.class.getName()).log(Level.SEVERE, null, ex); } path = "list_notes.jsp"; } // 插入操作 if ("insert".equals(status)) { // 1、接收插入的信息 String title = request.getParameter("title"); String author = request.getParameter("author"); String content = request.getParameter("content"); // 2、实例化Note对象 Note note = new Note(); note.setTitle(title); note.setAuthor(author); note.setContent(content); note.setErrors(errors); // 3、调用DAO完成数据库的插入操作 boolean flag = false; //验证标题、作者、内容是否为空 if (note.invalidate()) { try { DAOFactory.getNoteDAOInstance().insert(note); flag = true; request.setAttribute("flag", new Boolean(flag)); path = "insert_do.jsp"; } catch (Exception ex) { Logger.getLogger(NoteServlet.class.getName()).log(Level.SEVERE, null, ex); } } else { path = "insert.jsp"; request.setAttribute("errors", errors); request.setAttribute("note", note); } } // 按ID查询操作,为查看操作做准备 if ("look".equals(status)) { int n_id = Integer.parseInt(request.getParameter("n_id")); try { request.setAttribute("note", DAOFactory.getNoteDAOInstance().quaryById(n_id)); } catch (Exception ex) { Logger.getLogger(NoteServlet.class.getName()).log(Level.SEVERE, null, ex); } path = "look.jsp"; } // 按ID查询操作,修改之前需要将数据先查询出来 if ("quaryById".equals(status)) { int n_id = Integer.parseInt(request.getParameter("n_id")); try { request.setAttribute("note", DAOFactory.getNoteDAOInstance().quaryById(n_id)); } catch (Exception ex) { Logger.getLogger(NoteServlet.class.getName()).log(Level.SEVERE, null, ex); } path = "update.jsp"; } // 更新操作 if ("update".equals(status)) { int n_id = Integer.parseInt(request.getParameter("n_id")); // 1、接收更新的信息 String title = request.getParameter("title"); String author = request.getParameter("author"); String content = request.getParameter("content"); // 2、实例化VO对象 Note note = new Note(); note.setN_id(n_id); note.setTitle(title); note.setAuthor(author); note.setContent(content); //note.setErrors(errors); // 3、调用DAO完成数据库的插入操作 boolean flag = false; //if (note.invalidate()) { try { DAOFactory.getNoteDAOInstance().update(note); flag = true; request.setAttribute("flag", new Boolean(flag)); path = "update_do.jsp"; } catch (Exception ex) { Logger.getLogger(NoteServlet.class.getName()).log(Level.SEVERE, null, ex); } /*} else { request.setAttribute("note", note); path = "update"; }*/ } // 模糊查询操作 if ("quaryByLike".equals(status)) { String keyword = request.getParameter("keyword"); try { request.setAttribute("all", DAOFactory.getNoteDAOInstance().quaryByLike(keyword)); } catch (Exception e) { } path = "list_notes.jsp"; } // 删除操作 if ("delete".equals(status)) { // 接收参数 int n_id = Integer.parseInt(request.getParameter("n_id")); boolean flag = false; try { DAOFactory.getNoteDAOInstance().delete(n_id); flag = true; } catch (Exception ex) { Logger.getLogger(NoteServlet.class.getName()).log(Level.SEVERE, null, ex); } request.setAttribute("flag", new Boolean(flag)); path = "delete_do.jsp"; } } else { // 则表示无参数,非法的客户请求 path = "errors.jsp"; } request.getRequestDispatcher(path).forward(request, response); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -