JSP garbled summary and custom filter Encoding

Source: Internet
Author: User

1. Write a filter by yourself.

Charsetencodingfilter. Java

package filter;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;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 CharsetEncodingFilter implements Filter {private String encoding;public void destroy() {System.out.println("--------destroy---------");}public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {System.out.println("--------doFilter---------");arg0.setCharacterEncoding(encoding);arg2.doFilter(arg0,arg1);}public void init(FilterConfig arg0) throws ServletException {System.out.println("--------init---------");this.encoding = arg0.getInitParameter("encoding");System.out.println(encoding);}}

Web. xml configuration

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 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_2_5.xsd">  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <filter><filter-name>CharsetEncodingFilter</filter-name><filter-class>filter.CharsetEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>CharsetEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>    <filter-mapping><filter-name>CharsetEncodingFilter</filter-name><url-pattern>/servlet/*</url-pattern></filter-mapping></web-app>

2. on the page

<%@ page contentType="text/html;charset=UTF-8" %><%request.setCharacterEncoding("UTF-8");%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%response.setCharacterEncoding("UTF-8");%>

 

3. If a garbled like this occurs: % E4 % B8 % 8e % E6 % A8 % a1 % E5 % BC % 8f %

<%URLDecoder.decode(str,"UTF-8");%>

4. Manually convert Codes

String str = (String) request.getParameter("username");byte[] temp = str.getBytes("ISO-8859-1");str = new String(temp,"UTF-8");out.print(str);

5. Modify the default Tomcat encoding.

By default, Tomcat uses the encoding method of the iso8859-1
Modify the conf/server. xml file in Tomcat
Find the following code:
<Connector Port = "8080" protocol = "HTTP/1.1" connectiontimeout = "20000" redirectport = "8443"/>
This Code specifies the port number of Tomcat to listen to HTTP requests.
You can add an attribute here: uriencoding, which is set to a UTF-8 so Tomcat (default ISO-8859-1 encoding) can process get requests in UTF-8 encoding.
The changed code is as follows:
<Connector Port = "8080" uriencoding = "UTF-8" protocol = "HTTP/1.1" connectiontimeout = "20000" redirectport = "8443"/> In addition
You can also add the usebodyencodingforuri = "true" attribute to set the same encoding for post and get.
The changed code is as follows:
<Connector Port = "8080" usebodyencodingforuri = "true" uriencoding = "UTF-8" protocol = "HTTP/1.1" connectiontimeout = "20000" redirectport = "8443"/>

6. Pay attention to the location relationship between response. setcontenttype ("text/html; charset = UTF-8"); and printwriter out = response. getwriter,

Remember to put printwriter out = response. getwriter (); behind response. setcontenttype ("text/html; charset = UTF-8"); otherwise, the encoding will be invalid.

7. Others

Http://fiona777.iteye.com/blog/385934

Http://handonghandong.iteye.com/blog/758866

Http://gaobaolu.blog.edu.cn/2012/722247.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.