Filter solves the problem of garbled characters, blocks sensitive words, and controls page access rights.

Filter solves the garbled problem

Reference interface

public class filter implements Filter {
    
    
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    
    
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setCharacterEncoding("UTF-8");
        servletResponse.setContentType("text/html; Charset =utf-8");
        filterChain.doFilter(servletRequest,servletResponse);
    }
}

The interface here does not need to be fully implemented, only a doFilter needs to be implemented. Because there is defalut modification in the interface

public interface Filter {
    
    
    default void init(javax.servlet.FilterConfig filterConfig) throws javax.servlet.ServletException {
    
     /* compiled code */ }

    void doFilter(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse, javax.servlet.FilterChain filterChain) throws java.io.IOException, javax.servlet.ServletException;

    default void destroy() {
    
     /* compiled code */ }
}

Configure in xml

<filter>
      <filter-name>character</filter-name>
      <filter-class>com.yim.Servlet.filter</filter-class>
   </filter>
   <filter-mapping>
      <filter-name>character</filter-name>
      <url-pattern>/test2</url-pattern>
   </filter-mapping>

Here is the completion of the console and output to the page, there is no garbled problem, and there is no need to modify the garbled problem for each class.

Note: The writer garbled problem

FileReader reads garbled Chinese characters, pay attention to UTF-8

        InputStream inputStream= req.getInputStream();
        Reader reader=new InputStreamReader(inputStream, "UTF-8");
        BufferedReader bufferedReader=new BufferedReader(reader);

Block sensitive words

@WebFilter("/test")
public class WordFilter implements Filter {
    
    
 @Override
 public void doFilter(ServletRequest servletRequest, ServletResponse
servletResponse, FilterChain filterChain) throws IOException, ServletException
{
    
    
 servletRequest.setCharacterEncoding("UTF-8");
 //将"敏感词"替换成"***"
 String name = servletRequest.getParameter("name");
 name = name.replaceAll("敏感词","***");
 servletRequest.setAttribute("name",name);
 filterChain.doFilter(servletRequest,servletResponse);
 }
}

Control access to the page.

If the user directly opens the download link without logging in, it will not jump to the download page but jump to the login page.
Mainly use session and webfilter filters

@WebFilter("/download.jsp")
public class DownloadFilter implements Filter {
    
    
 @Override
 public void doFilter(ServletRequest servletRequest, ServletResponse
servletResponse, FilterChain filterChain) throws IOException, ServletException
{
    
    
 HttpServletRequest request = (HttpServletRequest) servletRequest;
 HttpServletResponse response = (HttpServletResponse) servletResponse;
 HttpSession session = request.getSession();
 String name = (String) session.getAttribute("name");
 if(name == null){
    
    
 //不是登录状态
 response.sendRedirect("/login.jsp");
 }else{
    
    
 filterChain.doFilter(servletRequest,servletResponse);
 }
 }
}

Guess you like

Origin blog.csdn.net/yimzuenmuanggg/article/details/113329309