Home >Java >JavaBase >Java web garbled solution

Java web garbled solution

尚
Original
2019-12-09 16:33:053368browse

Java web garbled solution

Set the saving encoding format of the file. Generally, when creating a new project, the GBK encoding format is set. At this time, the page encoding format of the file is GBK, and the saving format is ANSI. Later, after changing the encoding format of the project and files to UTF-8 encoding format, Chinese garbled characters still appeared. This is because the saving encoding format of the existing files is still ANSI, so the previous .java file must be saved. Change the format to utf-8 so that the problem of garbled Chinese comments will not occur

window-preferences-General-Content Types Select the corresponding file type under Text and then change the page encoding to UTF-8 and click Update. .

Note:

If it is a brand new workspace, setting the encoding format of Myeclipse is equivalent to setting the project, file and saving encoding format (excluding jsp, etc. File)

If it is an existing workspace and the original encoding format is GBK, then changing the encoding of the workspace is equivalent to setting the encoding format of the project and file, but for the previous The saving encoding of an existing .java file cannot be changed. It must be changed according to (4) above. However, the saving encoding of the new file is effective.

Several places to check for garbled characters

1. Saved encoding

2. Encoding declared in jsp (UTF-8 and utf8 are the same)

Note:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>

3 .The character encoding processed in the filter is limited to the input and output data encoding. The encoding format set in the above code is the display format of the page.

In a javaweb project code, there are a total of these formats:

1. Code saving format 2. Browser parsing format 3. Data input format (that is, Determined by the browser's parsing format) 4. The format during data processing.

Java web garbled solution

In principle, the above six formats must be the same, and there will never be garbled characters. However, there are some special cases, some of which are inconsistent, and the display will be correct. If not When displaying and processing the input content, the format of request and respose can be inconsistent with other formats. ps: Commonly used encodings are ISO-8859-1, gbk, utf-8

So the foolproof method is as follows:

The declaration statement in Jsp can get rid of four formats, and the remaining two formats can be solved using the listener method:

Create a new listener

Java web garbled solution

package com.helloweenvsfei.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;
 
public class CharacterEncodingFilter implements Filter{
	private String characterEncoding;	
	private boolean enabled;			
 
	public void destroy() {
		// TODO Auto-generated method stub
		characterEncoding = null;	
	}
 
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		if(enabled || characterEncoding != null){
			request.setCharacterEncoding(characterEncoding);
			
			response.setCharacterEncoding(characterEncoding);
			
		}
		chain.doFilter(request, response);
		
		
	}
 
	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
		
		characterEncoding = config.getInitParameter("characterEncoding");
		
		enabled = "true".equalsIgnoreCase(config.getInitParameter("enabled").trim());
		
		
	}
 
}

web.xml

<filter>
  	<filter-name>characterEncodingFilter</filter-name>
  	<filter-class>com.helloweenvsfei.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<param-name>characterEncoding</param-name>
  		<param-value>utf8</param-value>
  	</init-param>
  	<init-param>
  		<param-name>enabled</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  
  <filter-mapping>
  	<filter-name>characterEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Java web garbled solution. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn