当前位置:网站首页>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 {
}
}
边栏推荐
- mongodb 记录
- QT designer cannot modify the window size, and cannot change the size by dragging the window with the mouse
- Anti chicken soup speech
- Idea automatically generates serialVersionUID
- 【项目实训】线形箭头的变化
- [bull Chinese document] queue package used to process distributed jobs and messages in nodejs
- Common setup modes (Abstract Factory & responsibility chain mode & observer mode)
- redux Actions may not have an undefined “type“ property. Have you misspelled a constant?
- [graduation season · advanced technology Er] it's my choice. I have to walk on my knees
- /Bin/sh no such file or directory problem
猜你喜欢

【STL】关联容器之unordered_map用法总结

聚焦行业,赋能客户 | 博云容器云产品族五大行业解决方案发布

Summary of qvariant use in QT

网页制作存在的一些难点
![[daily training] 513 Find the value in the lower left corner of the tree](/img/97/ab2179d6dbd0536e8cc139659aecc2.png)
[daily training] 513 Find the value in the lower left corner of the tree

20220621 Dual Quaternion

云原生落地进入深水区,博云容器云产品族释放四大价值
![[STL] summary of pair usage](/img/ba/72697f0f8bf018f1b5884e9cc2be4e.png)
[STL] summary of pair usage

MySQL重做日志 redo log

Children's programming for comprehensively cultivating students' mental thinking
随机推荐
306. 累加数
[bull Chinese document] queue package used to process distributed jobs and messages in nodejs
MySQL optimization
core. What is JS ---kalrry
312. 戳气球
【项目实训】多段线扩充为平行线
MySQL MVCC多版本并发控制
为什么TCP协议是三次握手而不是两次?
746. 使用最小花费爬楼梯-动态规划
System permission program cannot access SD card
js 判断两个数组增加和减少的元素
Cloud box is deeply convinced to create a smart dual-mode teaching resource sharing platform for Nanjing No. 1 middle school
Copy and paste of idea without escape
图解三次握手四次挥手,小白都能看懂
Business logic design of physical warehouse and virtual warehouse in middle inventory
Cetos7 record
在金融行业做数据产品经理是什么体验
mysql 索引
897. 递增顺序搜索树
About professional attitude