創建一個過濾器:
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 {
@Override
public void init(FilterConfig filterConfig)
throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
servletRequest.setCharacterEncoding("UTF-8");
servletResponse.setContentType("text/html; charset=UTF-8");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
聲明它變成你的web.xml:
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>your.package.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
而且your're好到哪裏去。還請確保您的每個JSP
頁面都包含:<%@ page contentType="text/html;charset=UTF-8" language="java" %>
。如果您的應用程序在tomcat上運行,請確保您將URIEncoding="UTF-8"
屬性添加到Connector
元素。
你究竟試過了什麼?你需要解釋你準備做什麼。你提供的鏈接已經**了,使用UTF-8編碼,所以這個問題沒有意義。我不得不低估這個問題,因爲它是我在很長一段時間裏看到的最糟糕的研究問題之一。 –
什麼是頁面編碼?必須是<%@ page language =「java」contentType =「text/html; charset = UTF-8」pageEncoding =「UTF-8」%> .java文件也是UTF-8。 – gooogenot