Request object in JSP

Source: Internet
Author: User

1. garbled Chinese characters may occur when the request object processes get and post requests:
Tomcat processes get and post requests in different ways.
A POST request stores parameters in the message body of the request data packet.
Therefore, you can use request. setcharacterencoding ("UTF-8 ");
However, the GET request stores the parameters in the URL, and setcharacterencoding does not work at this time. At this time, we need to use handwritten code for transcoding. (Because Tomcat automatically performs iso8859-1 encoding on the GET request generated URL)
E.g .:

String name = new String(request.getParameter("username").getBytes("iso-8859-1"),"utf-8");

You can also modify the tomcat configuration:

<Connector port="8080" protocol="HTTP/1.1"               connectionTimeout="20000"               redirectPort="8443"        URIEncoding="UTF-8"        useBodyEncodingForURI="true"        />

But I tried it myself and somehow it seems ineffective ......


2. Execute forward and include for the request object
The httpservletrequest class provides a getrequestdispatcher (string path) method, where path is the target path of forward/include and returns requesdispatcher. This object provides the following two methods:

Forward (servletrequest request, servletresponse response): Execute forward; Include (servletrequest request, servletresponse response): Execute include;

The call is as follows:

request.getRequestDispatcher("/a.jsp").include(request, response);request.getRequestDispatcher("/a.jsp").forward(request, response);

The forward method has the same effect as the JSP: Forward label, but the include method has different effects with the JSP: Include label:
After the include method is called, the content in path is first loaded into the returned page, and then the content of the current page;
The JSP: Include label loads the content in path at any position.
Note that the character string in path must start.

Continue Learning, Hoho !~

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.