当前位置:网站首页>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");

 Insert picture description here

1.2、 Method 2

Use filters to solve .

 Insert picture description here

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 {
    

	}
}

原网站

版权声明
本文为[Study in a small corner]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230526441702.html