JSP three kinds of Chinese solutions and differences __jsp

Source: Internet
Author: User


Method One: It only works for a single string
String name=request.getparameter ("name");
Name=new String (name.getbytes ("iso-8859-1"), "Utf-8");


method Two: Valid for the current page
Request.setcharacterencoding ("GBK");
String name=request.getparameter ("name");

method Three: Valid for the entire Web site
Using filter principle
public class setencodingfilter implements filter {    protected  String encoding = null;     protected FilterConfig filterConfig = null;     protected boolean ignore = true;     public void destroy ()  {         this.encoding = null;         this.filterConfig = null;    &NBSP}     public void dofilter (ServletRequest request,  servletresponse response,              Filterchain chain)  throws ioexception, servletexception {         if  (ignore | |   (request.getcharacterencoding ()  == null))  {  &nbsP;         request.setcharacterencoding (selectEncoding (request));         }          Chain.dofilter (Request, response);    &NBSP}     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")                 | |  value.equalsignorecase ("yes")              this.ignore = true;         else              this.ignore = false;    &NBSP}     protected string selectencoding (ServletRequest  request)  {        return  (this.encoding)     &NBSP}     public filterconfig getfilterconfig ()  {         return filterConfig;    &NBSP}     public void setfilterconfig (FilterConfig  Filterconfig)  {        this.filterconfig = filterconFig    &NBSP}}

Web.xml
        <filter>          <filter-name>SetCharsetEncodingFilter</filter-name>          <filter-class>filter. Setencodingfilter</filter-class>         <init-param>              <param-name>encoding</param-name >             <param-value>gbk</ Param-value>         </init-param>     < /filter>     <filter-mapping>         < Filter-name>setcharsetencodingfilter</filter-name>          <url-pattern>/*</url-pattern>     </filter-mapping>


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.