2 Methods of character garbled processing in JSP

Source: Internet
Author: User

In the preparation of JSP programs, often encountered in the Chinese character processing problems, in the acceptance of request in the Chinese characters display a string of garbled. Online processing Method A basket, the following say I used two effective solutions:





1. Write a string-handling function for the program, save it in a static file, and include it in the JSP page that needs to handle the Chinese characters,





<%!


public string codetostring (String str)


 {


String S=str;


Try


  {


byte temp[]=s.getbytes ("Iso-8859-1");


s=new String (temp);


return s;


   }


catch (Exception e)


   {


return s;


   }


  }


%>





to convert only: coedetostring (Request.getparameter (..)); On it, it's good to use.





2. A universally applicable method, add a filter, then you can live once and for all.





package filters;


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;


import javax.servlet.http.HttpServletRequest;


import Javax.servlet.http.HttpServletRequestWrapper;


public class Tomcatformfilter implements Filter {


/**


* Request.java


* Expands the httpservletrequestwrapper, does not affect the original function and can provide the


some httpservletrequest

Functions in
* interface. It can be unified to solve the Chinese problem under the Tomcat default setting and only


Need to replace the
in the page with a new Request object

* Request object can be.


*/


class Request extends Httpservletrequestwrapper


{


Public Request (HttpServletRequest request) {


super (Request);


}


/**


* Converts the inner code of the data that is read by the form.


* Transfers from ISO characters to GBK.


*/


public string Tochi (string input) {


try {


byte[] bytes = input.getbytes ("iso8859-1");


return new String (bytes, "GBK");


}


catch (Exception ex) {


}


return null;


}


/**


* Return to the HttpServletRequest holded by this object.


*/


private HttpServletRequest gethttpservletrequest ()


{


return (HttpServletRequest) super.getrequest ();


}


/**


* Read Parameters--fixed the Chinese problem.


*/


public string GetParameter (string name)


{


return


Tochi (Gethttpservletrequest (). GetParameter (name));


}


/**


* Read the parameter list-fixed the Chinese issue.


*/


public string[] Getparametervalues (String name)


{


String values[] =


gethttpservletrequest (). getparametervalues (name);


if (values!= null) {


for (int i = 0; i < values.length; i++) {


Values[i] = Tochi (values[i));


}


}


return values;


}


}


public void Destroy () {


}


public void Dofilter (ServletRequest request, Servletresponse


response,


Filterchain chain) throws IOException, servletexception {


httpservletrequest httpreq = (httpservletrequest) request;


if (Httpreq.getmethod (). Equals ("POST")) {


request.setcharacterencoding ("GBK");


} else {


request = new request (HTTPREQ);


}


Chain.dofilter (request, response);


}


public void init (Filterconfig filterconfig) throws


servletexception {


}





Of course, remember to change the Web.xml





<filter>


<filter-name>TomcatFormFilter</filter-name>


<filter-class>filters. Tomcatformfilter</filter-class>


</filter>


 


<filter-mapping>


<filter-name>TomcatFormFilter</filter-name>


<url-pattern>/*</url-pattern>


</filter-mapping>





the above two methods have been tested, more useful

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.