current position:Home>Through the request forwarding command of the servlet program, will the access to the target resources protected by the filter filter be filtered by the filter?

Through the request forwarding command of the servlet program, will the access to the target resources protected by the filter filter be filtered by the filter?

2022-02-03 01:22:54 CSDN Q & A

Problems encountered :

  • adopt servlet Program request forwarding command , Access was Filter The target resource protected by the filter .

  • Hope to pass Filter Print statement added in , See if it passes through the filter . But it doesn't work , No results will be printed . Nor can it be transferred to the target resource

for jsp Page code

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title> Sign in </title></head><body><a href="admin/a.jsp"> Click the jump </a><form action="http://localhost:8080/_15_Filter/clickServlet" method="get">  user name :<input type="text" name="username" id="username"><br>  The secret &nbsp;&nbsp; code :<input type="password" name="password" id="password"><br> <input type="submit" value=" Submit "></form></body></html>

for Servlet Program
Submit to this page through the form of page code Servlet Program , Do not store user To Session in .

public class ClickServlet extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //  Solve the Chinese garbled code of the response data         resp.setContentType("text/html; charset=UTF-8");        //  To obtain parameters username        String username = req.getParameter("username");        //  To obtain parameters password        String password = req.getParameter("password");        //  Judge users , Does the password match         if ("123".equals(username) && "123".equals(password)){            req.getRequestDispatcher("admin/a.jsp").forward(req, resp);        } else {            //  Mismatch , Back to the login screen             req.getRequestDispatcher("login.jsp").forward(req, resp);        }    }}

for Filter Program
effect : Want to filter the access of users who have not logged in
Use Session Does the domain exist 'user', To determine whether to log in

public class AdminFilter implements Filter {    @Override    public void init(FilterConfig filterConfig) throws ServletException {    }    @Override    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {        //  Type conversion to HttpServletRequest、HttpServletResponse        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;        //  Filter whether users log in         Object user = httpServletRequest.getSession().getAttribute("user");        if (user == null){            //  If the user is not logged in , Then go to the login interface             System.out.println(" Was intercepted !!");            httpServletRequest.getRequestDispatcher("/logina.jsp").forward(httpServletRequest, httpServletResponse);        } else{           //  Let the program continue to access the user's target resources             System.out.println(" Successful visit !!");            filterChain.doFilter(servletRequest, servletResponse);        }    }    @Override    public void destroy() {    }}

Refer to the answer 1:

As long as it's done Filter Interface , All requests will be blocked

Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.

Random recommended