本文主要是介绍基于JSP+Servlet网上书城,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
开发环境:idea+mysql
使用技术:jsp+servlet
关键代码:
public class AdminServlet extends HttpServlet {private static final long serialVersionUID = 1L;//声明AdminService对象AdminService adminService=new AdminServiceImpl();protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取操作符String oper=req.getParameter("oper");//调用操作符对应方法if("adminLoginByAdminNameAndAdminPwd".equals(oper)) {//调用管理员登录方法adminLoginByAdminNameAndAdminPwdServlet(req,resp);}else if("adminQuit".equals(oper)) {//调用管理员退出的方法adminQuitServlet(req,resp);}}/*** 管理员退出登录* @param req* @param resp* @throws IOException * @throws ServletException */private void adminQuitServlet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//处理请求信息req.getSession().invalidate();//响应处理结果resp.sendRedirect(req.getContextPath()+"/adminjsps/login.jsp");return;}/*** 管理员登录功能方法* @param req* @param resp* @throws IOException* @throws ServletException*/private void adminLoginByAdminNameAndAdminPwdServlet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {//获取数据String adminName=req.getParameter("adminName");String adminPwd=req.getParameter("adminPwd");//处理请求信息Admin admin=adminService.findAdminByAdminNameAndAdminPwdDao(adminName, adminPwd);//响应处理结果if(admin!=null) {adminName=URLEncoder.encode(adminName,"utf-8");Cookie cookie=new Cookie("adminname",adminName);cookie.setMaxAge(3*24*3600);cookie.setPath(req.getContextPath()+"/adminjsps/login.jsp");resp.addCookie(cookie);req.setAttribute("name", adminName);req.getSession().setAttribute("admin", admin);resp.sendRedirect(req.getContextPath()+"/adminjsps/admin/index.jsp");return;}else {req.setAttribute("msg", "用户名或密码错误");req.getRequestDispatcher("/adminjsps/login.jsp").forward(req, resp);return;}}}
项目下载:基于JSP+Servlet网上书城
这篇关于基于JSP+Servlet网上书城的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!