当前位置:网站首页>Project_ Filter to solve Chinese garbled code
Project_ Filter to solve Chinese garbled code
2022-06-23 07:04:00 【Study in a small corner】
filter Filter Solve the Chinese garbled code
0、 Preface
Recently, I had a training class , The teacher asked to use the original Web Technology to develop back-end data interfaces . Seriously , Since using the frame , I haven't used these basic technologies for a long time , Although the principle is the same , It's just different usage .
How to put it? ? I think the foundation is still very important , The framework is highly encapsulated , It's really cool when you use it , But once the error is reported , If you don't know the principle , More difficult to locate and solve , So I personally think that reviewing the basics is not only helpful to understand the principle of the framework ,DEBUG Can also be more handy .
scene : Now there is a data interface , Find out the paging list of students , But there will be garbled code on the display browser , Next we give several solutions .
1、 resolvent
1.1、 Method 1
At each of the control layers Servlet Class doPost perhaps doGet Method by adding the following two lines of code .
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type", "textml;charset=utf-8");

1.2、 Method 2
Use filters to solve .

EncodingFilter.java The code is as follows :
package edu.zhku.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** * filter : When accessing the server , The filter intercepts the request and performs some special operations , Similar to water purifier . * Generally used to complete general operations * * @author Zhang * */
public class EncodingFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
/** * Every time a request is intercepted , Will execute . Execute many times */
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain filterChain) throws IOException, ServletException {
// Convert parent interface to child interface
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
// Get request method
String method = request.getMethod();
// solve post Request Chinese data garbled problem
if (method.equalsIgnoreCase("post")) {
request.setCharacterEncoding("utf-8");
}
// Deal with response garbled code
response.setContentType("text/html;charset=utf-8");
// Release request
filterChain.doFilter(request, response);
}
/** * After the server starts , Will create Filter object , And then call init Method . * Only once , Can be used to load resources */
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}
web.xml The code is as follows :
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<servlet-name>UserLoginServlet</servlet-name>
<servlet-class>edu.zhku.servlet.UserLoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>FindUserListByPage</servlet-name>
<servlet-class>edu.zhku.servlet.FindUserListByPage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserLoginServlet</servlet-name>
<url-pattern>/userLoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FindUserListByPage</servlet-name>
<url-pattern>/findUserListByPage</url-pattern>
</servlet-mapping>
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>edu.zhku.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<!-- Intercept path , Each access will be executed with the name “EncodingFilter” Filter -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
1.3、 Method 3
Method 3 Also use filters , Method 2 It's using xml Configuration mode . In addition, we can also use annotation , After using annotations, there is no need to configure xml 了 .
Annotate classes with annotations .
@WebFilter("/*")
public class EncodingFilter implements Filter{
}
take EncodingFilter.java The code is modified as follows :
package edu.zhku.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** * Intercept path /*, Block all * @author Zhang */
@WebFilter("/*") // annotation
public class EncodingFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
/** * Every time a request is intercepted , Will execute . Execute many times */
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain filterChain) throws IOException, ServletException {
// Convert parent interface to child interface
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
// Get request method
String method = request.getMethod();
// solve post Request Chinese data garbled problem
if (method.equalsIgnoreCase("post")) {
request.setCharacterEncoding("utf-8");
}
// Deal with response garbled code
response.setContentType("text/html;charset=utf-8");
// Release request
filterChain.doFilter(request, response);
}
/** * After the server starts , Will create Filter object , And then call init Method . Only once , Can be used to load resources */
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}
边栏推荐
猜你喜欢

MySQL重做日志 redo log

WPF command directive and inotifypropertychanged

Business logic design of physical warehouse and virtual warehouse in middle inventory

Storage mode of data in memory (C language)

MySQL basic query

Configuration and compilation of mingw-w64, msys and ffmpeg

Idea installing the cloudtoolkit plug-in

TP6+Redis+think-queue+Supervisor实现进程常驻消息队列/job任务

QT设计师无法修改窗口大小,无法通过鼠标拖动窗口改变大小的解决方案

【STL】pair用法总结
随机推荐
Idea installing the cloudtoolkit plug-in
2022年养老理财产品有哪些?风险小的
313. 超级丑数
The illustration shows three handshakes and four waves. Xiaobai can understand them
C language learning summary
【STL】关联容器之unordered_map用法总结
图解三次握手四次挥手,小白都能看懂
896. 单调数列
994. 腐烂的橘子-非递归法
[project training 10] drawing of arrows
307. 区域和检索 - 数组可修改
Copy and paste of idea without escape
Side effects of threads in embedded real-time systems
xml dtd 记录
C语言运算符优先级口诀
关于五险一金你需要知道的事情
cetos7 记录
MySQL function
GIS实战应用案例100篇(七十九)-多规整合底图的制作要点
Children's programming for comprehensively cultivating students' mental thinking