About getting form data

Source: Internet
Author: User
Document directory
  • Browser cache mechanism
  • Security

Today, a colleague encountered some small issues with form data when doing secondary development, but these problems are also difficult for me. Because we have been using struts to process form data for a long time. So I completely forgot it. I just want to learn something new.

The common method for obtaining JSP form data is request. getparameter. This method is used for post submission to obtain the message body information sent from the client to the server request. For get, it is used to obtain the parameter value after the URL. Either way, the parameters are the same, but one is displayed in the message body and the other is on the URL. This parameter is specified by name.

There has been a misunderstanding before, that request. getparameter can only get the parameters above the URL, is ignoring the items in the message body. Because formbean is used for a long time. These are all ignored.

If request. getparameter cannot obtain the value, there are three reasons.
1. The obtained request. getparameter parameter value does not exist in the form. That is, the name in no tag corresponds to the obtained parameter. Note that it is name rather than ID.
2. The enctype = "multipart/form-Data" attribute is set for the form. This is used when you need to upload files.
3. The obtained parameter tag contains Disabled = "disabled"

In the second case, it is troublesome to obtain the parameter value. Because the form data is encapsulated into Meta binary format for transmission. It seems that it can be solved through the common-fileupload component. Not tested. Search for the expected ones. Or there are other solutions.

Chinese questions:
When a form is submitted, the data flow defaults to a ISO-8859-1. Therefore, garbled characters may occur in Chinese when obtaining the value. At this time, you only need to use transcoding.

  1. String STR = request. getparameter ("username ");
  2. Byte B [] = Str. getbytes ("ISO-8859-1 ");
  3. STR = new string (B );

For more information, see request. getattribute.

This method is usually used by the server side to encapsulate beans and put them back to the view layer for further operations. In this case, the view layer uses getattribute to obtain data. In addition, the setattribute method is not necessarily a request. Refer to scope. Wider access options.

How to Select get or post

URL Length

The URL has a length limit, which generally does not exceed 255bytes .. different browsers have different length rules.
Therefore, the data volume must be considered when selecting a method for form submission. It is better to select post for displaying big data and unwanted data on URLs.

 

Browser cache mechanism

When using the get method, pay attention to the URL cache mechanism. Because the browser provides performance, if the two URLs delivered by the get method are the same, the cache mechanism will be enabled for the second time. Therefore, it is often necessary to transfer a virtual value to distinguish, in order to avoid obtaining a cache value for the second time.

For post, because the message body browser does not know the content of the message body, it cannot determine whether the information delivered twice is the same. Therefore, the cache mechanism is not enabled.

 

Security

For the post and GET request methods, the POST method is also relatively secure.

 

 

To sum up, in general cases, post can be used for post. However, the get method can bring us a lot of convenience in many cases, which is also indispensable.

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.