HttpServletResponse HttpServletRequest RequestDispatcher

Source: Internet
Author: User

HttpServletResponse HttpServletRequest RequestDispatcher
    • 07. Five
    • /
    • J2ee
    • /
    • No Comments

One, send Chinese data in bytes to the client
1. The server sends Chinese data in local default encoding
2, if the server sends the Chinese data to the client with UTF-8 encoding, the client must be informed of the encoding, otherwise the client will use the local default encoding for decoding, garbled problem.
Garbled solution appears:
When the server sends data by what encoding, it must tell the client what encoding to decode.
2.1 Browser "View"/"encode", change the default encoding (not advisable)
2.2 Setting the response header: Content-type text/html;charset=utf-8
2.3 Send to client <meta http-equiv= ' content-type ' content= ' text/html;charset=utf-8′>
2.4 Call Response.setcontenttype ("Text/html;charset=utf-8");
B. Send Chinese data to the client in character units
1. The server sends Chinese data with ISO-8859-1 as the default encoding.
2, the Chinese client receives the ISO-8859-1 encoded data certainly cannot decode normally (the Iso-8859-1 code table does not have the Chinese,??? )
Workaround:
A. Change the default encoding for sending data to the server
Response.setcharacterencoding ("UTF-8");
Also notify the client how to decode
Response.setheader ("Content-type", "Text/html;charset=utf-8″");

b, because of the frequent changes in coding, Serlvetresponse provides a better way to
Response.setcontenttype ("Text/html;charset=utf-8");
The effect of this method is equivalent to the two lines of code in a
Third, request redirection
1. Features:
A, the client issued 2 requests
b, the address bar will change
c, do not share the request and response objects
2, implementation: Response.sendredirect (URL);
Four, Response.getoutputstream and getwriter two methods of rejection. In a servlet method, either use the byte stream output or use a character stream output
The body content of the output of the stream that calls response is first written to the response object, the container extracts the response line, the response header, and the body from the response object, and the organization sends the client
Five, the request parameter's Chinese garbled question
1, Get request method (request parameter name and value passed to the server by the URL, the encoding is iso-8859-1)
There is no Chinese encoding in the Iso-8859-1 code table. To show normal, you need to encode it purely manually.
Principle: The byte code is obtained according to Iso-8859-1. byte b[] = Data.getbytes ("Iso-8859-1″");
Re-encodes according to the specified encoding. New String (b, "UTF-8");

2. Post Request mode
What is the encoding of the Web page, and what encoding is the requested data sent?
We can use Request.setcharacterencoding ("GBK") to re-specify the encoding regardless of the encoding of the page.

Six, the way of the wording:
A, absolute path notation: Serveltcontext must use absolute path. “/”
B, relative path: Relative paths can be used in other cases, or absolute paths can be used

C, when using absolute path, do not add "/" or the project name: If the server is called, do not add the project name, with "/" can represent the
If the client is called, the project name must be added

1. Forwarding: request.getrequestdispather (String URL)
Absolute path: Url/servlet/servletdemo
2. Redirect: Response.sendredirect (String URL)
Absolute path: Url/day05/servlet/servletdemo

3. Hyperlink: <a href= "url"/>
Absolute path: Url/day05/servlet/servletdemo
4. The path of the class-loaded file
Only relative paths can be used in Web applications, relative to the classes directory.
5, <fomr action= "url"/>
Absolute path:/day05/servlet/servletdemo
6, page sub-frame (frame)
Absolute path:/day05/servlet/servletdemo
7. Servletcontext.getrealpath (URL)
Absolute path: Url/servlet/servletdemo

HttpServletResponse HttpServletRequest RequestDispatcher

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.