Chinese character transfer from JSP to servlet processing

Source: Internet
Author: User
Tags filter
Js|servlet| Chinese for Request.getparameter () method in JSP and servlet, if you want to pass in
Chinese characters, there are several ways to solve,
One: Can be in the string temp = Request.getparameter ("xx");
temp = new String (temp.getbytes ("iso8859_1"));
Two: The commonly used method is to set a filter:
1. java files for Com.esoon.shabc.utils.SetCharacterEncodingFilter
Source file:
Package com.esoon.shabc.utils;

Import java.io.IOException;
Import java.io.UnsupportedEncodingException;
Import java.util.*;
Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.http.HttpServletRequest;

public class Setcharacterencodingfilter
Implements Filter {

protected String encoding = NULL;
protected Filterconfig filterconfig = null;
Protected Boolean ignore = true;

/**
* Take This filter out of service.
*/
public void Destroy () {
this.encoding = null;
This.filterconfig = null;
}

/**
* Select and set (if specified) the character encoding to is used to
* Interpret request parameters for this request.
*/
public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain)
Throws IOException, Servletexception {
Conditionally select and set the character encoding to be used
if (Ignore | | (request.getcharacterencoding () = null)) {
String encoding = selectencoding (request);
if (encoding!= null)
request.setcharacterencoding (encoding); Set the location where the request is encoded
}

Pass control in to the next filter
Pass control to next filter
Chain.dofilter (request, response);
}

/**
* Place this filter into service.
* Read the value of the initial parameter from the Web-app web.xml file
*/

public void init (Filterconfig filterconfig) throws Servletexception {
This.filterconfig = Filterconfig;
this.encoding = Filterconfig.getinitparameter ("encoding");
String value = filterconfig.getinitparameter ("Ignore");
if (value = = null)
This.ignore = true;
else if (Value.equalsignorecase ("true"))
This.ignore = true;
else if (value.equalsignorecase ("yes"))
This.ignore = true;
Else
This.ignore = false;
}

/**
* Select an appropriate character encoding to is used, based on the
* Characteristics of the current request and/or filter initialization
* Parameters. If no character encoding should is set, return
* <CODE>NULL</CODE>.
* Select the original code of request
*/
Protected String selectencoding (ServletRequest request) {
return (this.encoding);
}
}
2. Then add in the middle of the Web.xml file <web-app></web-app>:
<filter>
<filter-name>Set_Character_Encoding</filter-name>
<filter-class>com.esoon.shabc.utils.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set_Character_Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3. If the submitted field is inside the form, the form should be set to: Method= "POST"
Similar to: <form method= "POST" name= "Firstpriinfoform" ></form>
In this way, even if you submit multiple Chinese text segments, you need only
Request.getparameter ("# #");
Without having to convert new String () again.

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.