1. 程式人生 > >過濾器及請求封裝器實現字元設定

過濾器及請求封裝器實現字元設定

1、Html程式碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form method="post" action="hello">
        名稱:<input type="text" name="name"><br>
        年齡:<input 
type="text" name="age"><br> <input type="submit" value="post提交"> </form> <hr> <form method="get" action="hello"> 名稱:<input type="text" name="name" value=""><br> 年齡:<input type="text" name="age" value=""><br> <
input type="submit" value="get提交" name="submit"> </form> </body> </html>
html Code

2、Servlet程式碼

package com.test;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; @WebServlet(urlPatterns = "/hello") public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { // 讀取引數 String name = request.getParameter("name"); String age = request.getParameter("age"); // 響應客戶端 response.setContentType("text/html"); PrintWriter writer = response.getWriter(); writer.print(name + "|" + age); writer.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { doGet(request, response); } }
Java Code

3、請求封裝器

package com.test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.UnsupportedEncodingException;

public class MyRequestWrapper extends HttpServletRequestWrapper {
    // 構造方法不可少,且在方法內部必須呼叫父類的構造方法
    public MyRequestWrapper(HttpServletRequest request) {
        super(request);
    }
    @Override
    public String getParameter(String name) {
        HttpServletRequest request = (HttpServletRequest)getRequest();
        String parameter = request.getParameter(name);
        try {
            return new String(parameter.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
Java Code

4、過濾器

package com.test;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class EncodeDecodeFilter implements Filter {
    private FilterConfig filterConfig;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        // 首先解決響應亂碼
        response.setCharacterEncoding("UTF-8");
        // 解決請求亂碼
        // 如果是post請求,把原始請求物件傳遞進去
        MyRequestWrapper myRequestWrapper = new MyRequestWrapper(request);
        if ("POST".equals(request.getMethod())) {
            request.setCharacterEncoding("UTF-8");
            chain.doFilter(request, response);
            return;
        }
        // 如果是get請求,包裝請求物件傳遞進去
        chain.doFilter(myRequestWrapper, response);
    }
    public void destroy() { }
}
Java Code

6、測試方式

(1)瀏覽器輸入地址:http://127.0.0.1/index.html

(2)填寫表單時寫入中文字元,提交,檢視響應