2023-01-06
一、處理請求回應亂碼問題
通過過濾器處理亂碼問題
請求亂碼和回應亂碼
(1)創建一個"web Application"專案,命名為“bookstore06”,將"bookstore05"的代碼遷移過去,(點擊bookstore05專案中的“Show in Explorer”,將“resources”、“src”、“web”檔案夾復制到“bookstore06”中)
之后將“resources”設定為“資源目錄”

選中“web”檔案夾下中“WEB-INF”下的“lib”,將其中的包


洗掉“index.jsp”
(2)設定服務器(使用Tomcat8.5.27)
Name命名為“bookstore06_server”,設定為Chrome,Redeploy

(3)在“BaseServlet.java”中的“doGet”函式中的開頭添加代碼,用于處理回應亂碼
response.setContentType("text/html;charset=utf-8");
(4)之后將“CartServlet.java”中的第82行代碼(與上面的代碼相同)洗掉,
(5)創建一個過濾器“EncodingFilter”,

設定過濾器中的注解,添加urlPatterns="/*"
@WebFilter(filterName = "EncodingFilter",urlPatterns = "/*") public class EncodingFilter implements Filter { public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { req.setCharacterEncoding("utf-8");//處理post請求亂碼 resp.setContentType("text/html;charset=utf-8");//處理回應亂碼 chain.doFilter(req, resp);//之后放行 } public void init(FilterConfig config) throws ServletException { } public void destroy() { } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/541340.html
標籤:其他
