JSP form Processing

Source: Internet
Author: User
Tags url example

When it is necessary to obtain some information from the browser, in many cases, the Web server daemon is eventually given. The browser uses two methods to pass this information to the Web server. These methods are the Get method and the Post method.

GET Method:

The Get method appends the user information to the page request encoding. Are the pages and the encoded information separated? The characters are as follows:

http://www. Qixoo.com/hello?key1=value1&key2=value2

The Get method is the default method for passing information from the browser to the Web server and it produces a long string of addresses that are now in the browser: box. Do not use the Get method if you have a password or other sensitive information to pass to the server.

The Get method has a size limit: Only 1024 characters can be in a request string.

This information is accessed by using the QUERY_STRING header, which will pass the QUERY_STRING environment variable that it can use to process the GetParameter () method of the Request object using GetQueryString ().

POST Method:

The general reliable way to pass information to a backend program is the POST method.

This method is in exactly the same way as the Get method package information, but not after sending it as a text string? Send it as a separate message in the URL. This message comes from standard input and you can analyze and use your processing form in the backend program.

The JSP handles this type using the GetParameter () method to read the simple parameters and the getInputStream () method to read the request from the client's binary data stream.

Reading form data using JSPs

The processing of form data in JSPs is automatically analyzed using the following methods, as appropriate:

    • GetParameter (): Call the Request.getparameter () method to get the value of the form parameter.

    • Getparametervalues (): Call this method if the parameter appears more than once and returns multiple values, such as a check box.

    • Getparameternames (): Call this method if you want a complete list of all parameters in the current request.

    • getInputStream (): Call this method to read the binary data stream from the client.

The Get method uses the URL example:

Here is a simple URL that will use the Get method to pass a two-value helloform program.

Http://localhost:8080/main.jsp?first_name=ZARA&last_name=ALI

The following is the main.jsp JSP program to process the input in the Web browser given. We will Usegetparameter () method, which makes it very easy to get the message passed:

Now the type Http://localhost:8080/main.jsp?first_name=ZARA&last_name=ALI in your browser ' s location:box. This will produce the following results:

Using GET Method to Read Form Data
  • First Name:zara

  • Last Name:ali

The Get Method example uses the form:

The following is a simple example of using an HTML table and a submit button to pass two values. We will use the same JSP main.jsp to process this input.

Please place the hello.html in this HTML file and place it in the Webapps/root directory of the <tomcat-installation-directory>/. When you will be accessing http://localhost:8080/Hello.html, here is the actual output of the above form.

Try entering first name and last name, and then click the Submit button to see the results where Tomcat is running on the local machine. A similar result is produced in the above example, based on the input supplied as input.

The Post Method example uses the form:

Let's make a small modification to the JSP above to handle the Get and post methods. The following is a main.jsp JSP program that handles input using the Get or post method given through a Web browser.

In fact, there is no change in the JSP above, because the only way to pass parameters is to change, no binary data is passed to the JSP program. The concepts associated with file processing will be explained in a separate chapter where we need to read the binary data stream.

The following is the contents of the hello.html file:

Now let's save main.jsp and hello.html in <tomcat-installation-directory>/'s Webapps/root directory. When you will be accessing http://localhost:8080/Hello.html, the following is the actual output of the form above.

Try entering the first name and last name, and then click the Submit button to see the results of the Tomcat running on the local machine.

Based on the input provided as input, in the example above, it produces similar results.

Through the check box data to the JSP program

Check boxes are used when multiple options are required to be selected.

Here is an example of the HTML code, checkbox.html, with two checkboxes of the form

The result of this piece of code is the following form

The following is a main.jsp JSP program to handle the input of the Web Browser check box button issued.

For the example above, this will show the following results:

Reading Checkbox Data
  • Maths Flag:: On

  • Physics Flag:: null

  • Chemistry Flag:: On

Read all form parameters:

The following is a general example, which uses the HttpServletRequest Getparameternames () method to read all available form parameters. This method returns an enumeration that contains a variable sequence of parameter names.

Once we have a enumeration, we can loop down the enumeration in the standard way, using the call hasMoreElements () method to determine when to stop using the and Nextelement () method to get the name of each parameter.

<%@ page import= "java.io.*,java.util.*"%>

Here's what's hello.html:

Now that you try to invoke the JSP using the above hello.html, this will produce a similar result, based on the input provided:

Read all form parameters
Param Name Param Value (s)
Maths On
Chemistry On

JSP form Processing

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.