当前位置:网站首页>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 {
}
}
边栏推荐
猜你喜欢

406-双指针(27. 移除元素、977.有序数组的平方、15. 三数之和、18. 四数之和)

js 判断两个数组增加和减少的元素

Why does TCP protocol shake hands three times instead of two?

Linux Installation mysql8.0.25

【STL】pair用法总结
![[STL] unordered of associated container_ Map Usage Summary](/img/6a/d614f2f363fa5181c25e79ff8b0dab.png)
[STL] unordered of associated container_ Map Usage Summary
![[STL] summary of map usage of associated containers](/img/1d/1b6488ea47face0548500b1e1ec60d.png)
[STL] summary of map usage of associated containers

mysql 基础查询

产品-Axure9(英文版),原型设计 制作下拉二级菜单

EndNote20使用教程分享(未完
随机推荐
Chrome remove duplicate bookmarks
[STL] summary of map usage of associated containers
关于五险一金你需要知道的事情
Anti chicken soup speech
【STL】容器适配器之stack、queue用法总结
Summary of qvariant use in QT
EndNote20使用教程分享(未完
产品-Axure9(英文版),原型设计后台动态二级菜单显示内容
excel高级绘图技巧100讲(八)-Excel绘制WIFI图
[shell] tree command
C language operator priority formula
Copy and paste of idea without escape
Linux Installation mysql8.0.25
网页制作存在的一些难点
Some difficulties in making web pages
[saison de remise des diplômes · technologie agressive er] votre choix, agenouillez - vous et partez
js 判断两个数组增加和减少的元素
cetos7 记录
Centos7 MySQL records
QT method of compiling projects using multithreading