Method 1: JSP development is the simplest and most commonly used method.
- <%@ page language="java" pageEncoding="GBK" %>
Or you can use gb2312 or gbk here, But gbk supports more than gb2312 characters.
This method is used to display Chinese characters on the jsp page.
Method 2: Use a filter
Filters are mainly used for form submission. Are the data inserted into the database? . This is also for tomcat not according to the request specified encoding, or self-made adopts the default encoding mode iso-8859-1 encoding.
Compile a SetCharacterEncodingFilter class.
- 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 SetCharacterEncodingFilter implements Filter {
- Protected StringEncoding=Null;
- Protected FilterConfigFilterConfig=Null;
- Protected booleanIgnore=True;
- Public void init (FilterConfig filterConfig) throws ServletException {
- This. filterConfig= FilterConfig;
- This. encoding=FilterConfig. GetInitParameter ("encoding ");
- StringValue=FilterConfig. GetInitParameter ("ignore ");
- If (Value= Null)
- This. ignore=True;
- Else if (value. inclusignorecase ("true "))
- This. ignore=True;
- Else
- This. ignore=False;
- }
- Public void doFilter (
- ServletRequest request, ServletResponse response, FilterChain chain)
- Throws IOException, ServletException {
- // TODO automatically generates method stubs
- If (ignore (request. getCharacterEncoding () = null )){
- StringEncoding=SelectEncoding(Request );
- If (encoding! = Null)
- Request. setCharacterEncoding (encoding );
- }
- Chain. doFilter (request, response );
- }
- Public void destroy (){
- // TODO automatically generates method stubs
- This. encoding=Null;
- This. filterConfig=Null;
- }
- Protected String selectEncoding (ServletRequest request ){
- Return (this. encoding );
- }
- }
Then, add web. xml
- <!-- Set Character Encoding-->
- <filter>
- <filter-name>Set Character Encoding</filter-name>
- <filter-class>com.struts.common.SetCharacterEncodingFilter
- </filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>Set Character Encoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- Set Character Encoding-->
JSP development has many advantages in using filters, especially in projects.
And it is more useful when using internationalization, as long as the page specifies <% @ page language = "java" pageEncoding = "UTF-8" %>, the server displays the correct character set according to the local Locale.
Therefore, we recommend that you use filters.
Method 3: Modify URIEncoding In the tomcat server. xml file.
- <Connector debug="0" acceptCount="100" connectionTimeout="20000"
disableUploadTimeout="true" port="80" redirectPort="8443"
enableLookups="false" minSpareThreads="25" maxSpareThreads="75"
maxThreads="150" maxPostSize="0" URIEncoding="GBK" >
- </Connector>
This method is mainly used to obtain strings from URLs.
JSP is developed in tomcat5.0 and later versions. The post and get methods are different in coding. If you get Chinese characters in the url, what will happen? . But there is no problem in tomcat4.1, because the post and get methods of tomcat4.1 are the same in encoding.
- Jsp xml xslt converts output to HTML
- Analysis on single processing and multi-task processing of JSP Technology
- JSP variables are called range variables in the specification.
- Detailed explanation of JSP technology methods
- JSP variables are called range variables in the specification.