Several Chinese solutions for JSP __jsp

Source: Internet
Author: User

To learn the Java-ee so far, the project has only done two, midway encountered some problems, the most likely to be a number of Chinese display and insert the database of Chinese problems. The summary is as follows:

(a) Define the Chinese conversion method:

New JSP page, named Tochinese.jsp can also be other;

The method is defined as:

<%!

String Tochinese (String str) {

String rs = null;

Byte[] temp;

try {

temp = getBytes ("iso8859-1");

rs = new String (temp, "gb2312");

}

catch (Java.io.UnsupportedEncodingException e) {

E.printstacktrace ();

}

Return RS;

}

String Tochinese (Object obj) {

Return Tochinese (Obj.tostring ());

}

%>

In the JSP page, you need to use the Chinese translation of the place to introduce the page, and then use this method to convert.

Such as: <%@ include file= "tochinese.jsp"%>

String content = Tochinese (Request.getparameter ("tcontent"));

PS: This method is a more conservative approach. Because no matter how the system is transplanted anywhere, it can be used.

(ii) Set up HTTP inquiries as GB2312:

In each place where Chinese translation is required: request.setcharacterencoding ("gb2312");

All parameters passed by post or get are encoded in GB2312 mode.

PS: This method is convenient, but also causes the duplication of coding. Reusability of the differential.

(iii) Write filter filters so that all HTTP queries for all pages are encoded in GB2312 way:

Writing filters


Package com.pharm.filter;

Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.IOException;

/**
* Character encoding filter <br>
*
* Let request be Simplified Chinese GB2312
*
* @author fuchun--ying
* @version 1.1
*/
public class Encodingfilter implements Filter {
protected Filterconfig filterconfig;
Private String targetencoding = "gb2312";

/**
* Initialize the filter like a normal servlet, it can also get the initial parameters
*
* @throws servletexception If an error occurred.
*/
public void init (Filterconfig config) throws servletexception {
this.filterconfig = config;
this.targetencoding = Config.getinitparameter ("encoding");
}

/**
* Filter processing, this method is the most important, but also the core code of filter
*
* @throws IOException If an error occurred.
* @throws servletexception If an error occurred.
*/
public void Dofilter (ServletRequest srequest,servletresponse sresponse,filterchain chain)
Throws Ioexception,servletexception {

System.out.println ("Encode HTTP requests using the following methods: Encoding=" +targetencoding);

HttpServletRequest request = (httpservletrequest) srequest;
Request.setcharacterencoding (targetencoding);
Chain.dofilter (Srequest,sresponse);
}

/**
* Set Filter log
*/
public void Setfilterconfig (final filterconfig filterconfig) {
This.filterconfig = Filterconfig;
}

/**
* Implement the Filter method, destroy the filter
*/
public void Destroy () {
This.filterconfig = null;
}
}

Described in Web.xml:
<filter>
<filter-name>encoding</filter-name>
<filter-class>EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

PS: Very good reusability.

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.