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.
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
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!

The article discusses various Java garbage collection algorithms (Serial, Parallel, CMS, G1, ZGC), their performance impacts, and suitability for applications with large heaps.

The article discusses the Java Virtual Machine (JVM), detailing its role in running Java programs across different platforms. It explains the JVM's internal processes, key components, memory management, garbage collection, and performance optimizatio

Java's Nashorn engine enables JavaScript scripting within Java apps. Key steps include setting up Nashorn, managing scripts, and optimizing performance. Main issues involve security, memory management, and future compatibility due to Nashorn's deprec

Java's try-with-resources simplifies resource management by automatically closing resources like file streams or database connections, improving code readability and maintainability.

Java enums represent fixed sets of values, offering type safety, readability, and additional functionality through custom methods and constructors. They enhance code organization and can be used in switch statements for efficient value handling.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools