欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

超市订单管理系统SMBMS

时间:2023-08-01
文章目录

登录功能优化

注销功能:

Servlet代码xml注册 登录拦截优化

编写一个过滤器xml注册过滤器 登录功能优化 注销功能:

思路:移除session,返回登录页面

Servlet代码

package com.yang.servlet.user;import com.yang.util.Constants;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;public class LogoutServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //移除用户的session req.getSession().removeAttribute(Constants.USER_SESSION); resp.sendRedirect("/login.jsp");//返回登录页面 } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }}

xml注册

LogoutServlet com.yang.servlet.user.LogoutServlet LogoutServlet /jsp/logout.do

登录拦截优化 编写一个过滤器

package com.yang.filter;import com.yang.pojo.User;import com.yang.util.Constants;import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;public class SysFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; //从session中获取用户 User user = (User) request.getSession().getAttribute(Constants.USER_SESSION); if (user==null){//已经被移除或者注销,或者未登录 response.sendRedirect("smbms/error.jsp"); }else { chain.doFilter(req,resp); } } public void destroy() { }}

xml注册过滤器

SysFilter com.yang.filter.SysFilter SysFilter /jsp/*

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。