Web Communication in Java

Source: Internet
Author: User

First, head, Get,post request Way main difference

Head only requires the server to return the header field of the HTTP response message

    1. The first is that "get-committed data can be up to 1024 bytes" because get commits data through a URL, so the amount of data that get can commit is directly related to the length of the URL. In fact, the URL does not have the upper limit of the argument, the HTTP protocol specification does not limit the length of the URL. This restriction is restricted to specific browsers and servers. IE's limit on URL length is 2083 bytes (2k+35). For the Get method browser to put the field information in the submission form in the request header , not suitable for the transfer of large data volume, post mode browser will submit the field information in the form in the request body , suitable for large data transfer, such as: Upload files to the server side, etc.

2. Since tomcat5.x started, the Get and post methods have submitted information, Tomcat There are different ways to process the encoding, and for the POST request, Tomcat will still be processed using the encoding set by the Request.setcharacterencoding method, and if not set, the default Iso-8859-1 encoding is used. And the GET request is different, Tomcat does not take into account the encoding set by the Request.setcharacterencoding method for a GET request, but will always use iso-8859-1 encoding, and the friend uses exactly the GET request, so Tomcat will use the bytes that iso-8859-1 will commit Converted to a string.

There are two ways to solve this problem:
Change the GET request to a POST request, and then you can use
the request.setcharacterencoding method to set the encoding and use the The Request.getparameter method directly obtains the Chinese request parameter.
Instead of changing the GET request, use the following code in the servlet to get the Chinese request parameter.

String name = new String (Request.getparameter ("name"). GetBytes ("Iso-8859-1"), "GBK");

In summary, if a GET request is used, the Setcharacterencoding method does not work and can only be resolved using the code above, and the POST request, although the setcharacterencoding method works, But using the code above still works (at this point you cannot use the Setcharacterencoding method to format the encoding in a non-iso-8859-1 format). Therefore, if you want the servlet to be able to handle the Chinese information in the Get and post requests at the same time, in addition to judging these two methods, you can also use the above code to process the Chinese information of both requests simultaneously.


The author recommends using the following code to obtain the Chinese request parameters:

String name = new String (Request.getparameter ("name"). GetBytes ("Iso-8859-1"), "GBK");

Because the code above takes advantage of Java's coding capabilities, it is valid for all Web servers, and the Setcharacterencoding method is supported by the Web server, and not all Web servers will support this method well.


Ii. Scoket and HttpURLConnection Communications


Socket is a TCP and UDP programming interface, you can use it to establish a TCP connection and so on. While the TCP and UDP protocols belong to the Transport layer , and HTTP is an application-level protocol, it is actually built on top of the TCP protocol.

(HTTP is a sedan that provides a specific form of encapsulation or display of data; the socket is the engine that provides the ability to communicate on the network.) )


HttpURLConnection internal use of buffer technology, when transferring files, files written to memory buffer, easy memory overflow



This article is from the "Nothing-skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1656566

Web Communication in Java

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.