JSP uses a filter to solve the request getParameter Chinese garbled problem, requestparameter

Source: Internet
Author: User

JSP uses a filter to solve the request getParameter Chinese garbled problem, requestparameter

(1) client data is generally submitted to the server through http get/POST. When the server uses request. getParameter () to read parameters, Chinese garbled characters may easily occur.

(2) Use a filter to solve the Chinese garbled request.

(3) The Code is as follows:

Package my; import java. io. *; import javax. servlet. *; import javax. servlet. http. *; public class ChineseFilter implements Filter {// defines a Filter implementation Filter interface private FilterConfig = null; public void init (FilterConfig config) throws ServletException {this. config = config;} public void destroy () {config = null;} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {request. setCharacterEncoding ("GB2312"); chain. doFilter (request, response); // forward the filtered request object to the next filter }}

(4) Deploy the filter. Edit the WEB-INF \ web. xml file and add the following:

<filter> <filter-name>cf</filter-name> <filter-class>my.ChineseFilter</filter-class> </filter> <filter-mapping> <filter-name>cf</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> </filter-mapping>

The <dispatcher> </dispatcher> here is mainly used with RequestDispatcher.

1. When the value is REQUEST, the filter can be activated only when a REQUEST comes directly from the client. If the REQUEST comes from RequestDispatcher. forward, It is not activated;

2. If the value is FORWARD, the filter is activated if the request is from RequestDispatcher. forward;

3. When the value is INCLUDE, this filter is activated if the request is from RequestDispatcher. include;

4. When the value is "ERROR", it indicates that this filter is activated only when the request is from RequestDispatcher using the "ERROR information page;

5. The default value is REQUEST.

(5) create a jsp page for verification

<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

(6) OK! We hope you have succeeded!


Jsp page requestgetParameter (""); get Chinese garbled characters. After the garbled problem is solved, if it is null, an error is returned. How can this problem be changed?

Method 1
Request. setCharacterEncoding ("GBK ");

Method 2
String a = request. getParameter ("");
If (! = Null ){
A = new String (a. getBytes ("ISO8859_1"), "GBK ");
}

Chinese garbled characters appear in requestgetparameter ()

If the Chinese characters extracted using request. getParameter in JSP are garbled characters, you only need to add the following code to the jsp file that processes the Chinese characters.
<%
Request. setCharacterEncoding ("GBK/GB2312 ");
// Set the encoding format to Chinese
String title = request. getParameter ("title ");
// The parameters in the brackets are optional, but the double quotation marks are indispensable.
Out. print (title );
%>
Note: The preceding code must be added to the <title> </title> mark.

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.