English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The example is as follows:
public class LoginCheckFilter implements Filter { private FilterConfig config=null; private String webroot=null; public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request=(HttpServletRequest)req; HttpServletResponse response=(HttpServletResponse)resp; HttpSession session=request.getSession(false); //Get the current session's session, do not create a session if there is no session, the same as request.getSession()} String url = request.getRequestURI(); //Get the current input /Project/Whether the current access path is the same as the path defined by us if (url != null && url.equals(webroot+"/login.jsp") && url.equals(webroot+"/LoginServlet")){ chain.doFilter(req, resp); } else { if (session == null) { response.sendRedirect(webroot+"/login.jsp } else { String user2=(String) session.getAttribute("user1); if (user2== null) { response.sendRedirect(webroot+"/login.jsp } else { chain.doFilter(request, response); } } } } @Override public void init(FilterConfig config) throws ServletException { // TODO Auto-generated method stub this.config = config; ServletContext ctx = config.getServletContext(); //Get URL /Project Name webroot = ctx.getContextPath(); //Project Name } }
It should be noted here that the jump used by LoginServlet to jump to the welcome page is a client-side jump.
This article, which details the writing method of the filter selection in jsp, is all the content shared by the editor. It is hoped that it can provide a reference for everyone, and everyone is also expected to support the Shouting Tutorial more.