Filter to solve the problem of Chinese garbled characters

Write a filter

@WebFilter(urlPatterns = "/filter/*")
public class HelloFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        //chain.doFilter: continue. If you do not execute this statement, you will terminate the access to the target
        chain.doFilter(request,response);
    }

    @Override
    public void destroy(a) {}}Copy the code

Write a simple servlet

@WebServlet(urlPatterns = {"/hello","/filter/hello"})
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("Hello, how are you?);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }}Copy the code

The code above means that if the HelloServlet is accessed via /filter/hello, it will pass through the filter, otherwise it will not pass through the filter

The filter is used to solve the problem of garbled characters

If you call /filter/hello, there will be no Chinese garbled characters, while if you call /hello, there will be Chinese garbled characters

The result is as follows:

Use filters to implement permission access

For example, some resources must be logged in to use

So you can verify it in Filter

There are four pages in total, of which the success.html file requires login to access

The following code

Filter:

@WebFilter(urlPatterns = "/success.html")
public class LoginFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        Object isLoginObject =  req.getSession().getAttribute("isLogin");
        if(isLoginObject ==null){
            res.sendRedirect("/error.html");
        }else {
           if((boolean) isLoginObject){
               chain.doFilter(req,res);
           }else {
               res.sendRedirect("/error.html"); }}}}Copy the code

The login Servlet

@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        HttpSession session = req.getSession();
        if(username.equals("kehao")&&password.equals("123456")){
            session.setAttribute("isLogin".true);
            resp.sendRedirect("/success.html");
        }else {
            session.setAttribute("isLogin".false);
            resp.sendRedirect("/error.html"); }}@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }}Copy the code

Logout Servlet

@WebServlet(urlPatterns = "/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        session.setAttribute("isLogin".false);
        resp.sendRedirect("/login.html");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }}Copy the code

The login page

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>The login</title>
</head>
<body>
<form action="/login" method="post">
    <input type="text" name="username" value=""><br>
    <input type="password" name="password" value=""><br>
    <input type="submit" value="Login">
</form>

</body>
</html>
Copy the code