三.四 编写后台控制
后台类结构:
三.四.一 跳转到登录的类
@WebServlet("/User/toLogin") public class UserToLoginServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(req, resp); } }
三.四.二 登录类
@WebServlet("/User/login") public class UserLoginServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //找到相应的Subject Subject subject=SecurityUtils.getSubject(); UsernamePasswordToken token=new UsernamePasswordToken(req.getParameter("code"), req.getParameter("password")); try{ subject.login(token); boolean2Json(resp, true); }catch(Exception e){ //代码为001,表示用户名或者密码错误 map2Json(resp,"001"); } } /** * 将状态返回到前台,通常是添加,删除,更新的操作,如果错误,则传入错误代码。 * @param o * @param exclueds */ public void map2Json(HttpServletResponse resp,String ... code){ //指定哪些属性不需要转json JSONObject objMap=new JSONObject(); if(code==null||code.length<1){ objMap.put("status",true); }else{ objMap.put("status",false); objMap.put("error_code",code[0]); } resp.setContentType("text/json;charset=utf-8"); try { resp.getWriter().print(objMap.toString()); } catch (IOException e) { e.printStackTrace(); } } /** * 传入是否成功,只返回状态 * @param o * @param exclueds */ public void boolean2Json(HttpServletResponse resp,boolean flag){ //指定哪些属性不需要转json JSONObject objMap=new JSONObject(); objMap.put("status",true); objMap.put("flag",flag); resp.setContentType("text/json;charset=utf-8"); try { resp.getWriter().print(objMap.toString()); } catch (IOException e) { e.printStackTrace(); } } }
注意,这两个Json 转换的方法是通用的,每一个需要转换的类里面都有,避免代码过多,老蝴蝶这不重复写了。
三.四.三 登录成功后跳转到主页
@WebServlet("/Main/toMain") public class MainServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(req, resp); } }
三.四.四 退出方法
@WebServlet("/User/logout") public class UserLoginOutServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Subject subject=SecurityUtils.getSubject(); //退出登录 subject.logout(); req.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(req, resp); } }
三.四.五 跳转到员工页面
@WebServlet("/User/toList") public class UserToListServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getRequestDispatcher("/WEB-INF/pages/user.jsp").forward(req, resp); } }
三.四.六 员工添加
@WebServlet("/User/add") public class UserAddServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("执行添加的方法"); boolean2Json(resp, true); } ... //json 转换的方法,具体见登录方法 }
三.四.七 员工修改
@WebServlet("/User/update") public class UserUpdateServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("执行修改的方法"); boolean2Json(resp, true); } }
三.四.八 员工删除
@WebServlet("/User/delete") public class UserDeleteServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("执行删除的方法"); boolean2Json(resp, true); } }
三.四.九 员工查询
@WebServlet("/User/select") public class UserSelectServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("执行查询的方法"); boolean2Json(resp, true); } }