Html in jsp file

Java Guides

This article is a series of JSP Tutorial . In this article, we will learn how to make use of HTML forms with JSP. We will build a complete Student Registration HTML Form with input button, checkbox button, radio button, and dropdown list etc. Along with this, we will also learn how to read HTML form elements data using request.getParameter() method.

The Methods in Form Processing

GET method

The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character as follows −

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

The GET method is the default method to pass information from the browser to the web server and it produces a long string that appears in your browser’s tab. It is recommended that the GET method is better not used. if you have a password or other sensitive information to pass to the server.

POST method

This method packages the information in exactly the same way as the GET method, but instead of sending it as a text string after a ? in the URL it sends it as a separate message. This message comes to the backend program in the form of the standard input which you can parse and use for your processing.

Читайте также:  Php file opening as text

JSP handles this type of requests using getParameter() method to read simple parameters and getInputStream() method to read binary data stream coming from the client.

Reading Form Data using JSP

getParameterValues() − Call this method if the parameter appears more than once and returns multiple values, for example — checkbox.

getParameterNames() − Call this method if you want a complete list of all parameters in the current request.

Reading HTML Form Data with JSP

  1. Read HTML forms input text field value in JSP
  2. Read HTML forms select tag (drop-down list) value in JSP
  3. Read HTML forms radio-button field value in JSP
  4. Read HTML forms checkbox tag value in JSP

1. Read HTML Form Input Text field Value

In this example, we will build an HTML form to read student information. Let’s first create a simple HTML form with input field elements. Later we will read HTML form data using JSP.

student-form.html

html> head> title>Student Registration Formtitle> head> body> form action="student-response.jsp" method="post"> First name: input type="text" name="firstName" /> br/>br/> Last name: input type="text" name="lastName" /> br/>br/> input type="submit" value="Submit" /> form> body> html>

Output

Now, we have created a simple Student registration page, in order to read from data we will create a student-response.jsp page as explained below.

student-response.jsp

  1. We use request.getParameter() method to get the value of a form parameter.
  2. We can also use $ expression language to read the input value.
  3. We are using an expression tag to display a result.
html> head>title>Student Confirmation Titletitle>head> body> ul> li>p>b>First Name:b>  request.getParameter("firstName") %> p>li> li>p>b>Last Name:b>  request.getParameter("lastName") %> p>li> ul> body> html>

Output

2. Read HTML forms select box (drop-down list) value in JSP

student-dropdown-form.html

html> head> title>Student Registration Formtitle> head> body> form action="student-dropdown-response.jsp"> First name: input type="text" name="firstName" /> br/>br/> Last name: input type="text" name="lastName" /> br/>br/> select name="country"> option>Braziloption> option>Franceoption> option>Germanyoption> option>Indiaoption> option>Turkeyoption> option>United Kingdomoption> option>United States of Americaoption> select> br/>br/> input type="submit" value="Submit" /> form> body> html>

Deploy this page on tomcat server and hit respective URL in the browser will display below form on the browser:

Let’s create a student-dropdown-response.jsp which will read above form data and display on a screen. Here is complete code in a page.

  1. We use request.getParameter() method to get the value of a form parameter.
  2. We can also use $ expression language to read the input value.
  3. We are using an expression tag to display a result.
html> head> title>Student Confirmation Titletitle> head> body> ul> li>p>b>First Name:b>  request.getParameter("firstName") %> p>li> li>p>b>Last Name:b>  request.getParameter("lastName") %> p>li> ul> br /> br /> The student's country: $param.country> body> html>

3. Read HTML Form’s radio button value data

student-form-radio.html

html> head> title>Student Registration Formtitle> head> body> form action="student-radio-response.jsp"> First name: input type="text" name="firstName" /> br/>br/> Last name: input type="text" name="lastName" /> br/>br/> Favorite Programming Language: br/> input type="radio" name="favoriteLanguage" value="Java"> Java input type="radio" name="favoriteLanguage" value="C#"> C# input type="radio" name="favoriteLanguage" value="PHP"> PHP input type="radio" name="favoriteLanguage" value="Ruby"> Ruby br/>br/> input type="submit" value="Submit" /> form> body> html>

Let’s create a student-radio-response.jsp which will read above form data and display on a screen. Here is complete code in a page.

  1. We use request.getParameter() method to get the value of a form parameter.
  2. We can also use $ expression language to read the input value.
  3. We are using an expression tag to display a result.
html> head>title>Student Confirmation Titletitle>head> body> ul> li>p>b>First Name:b>  request.getParameter("firstName") %> p>li> li>p>b>Last Name:b>  request.getParameter("lastName") %> p>li> ul> br/> br/> The student's favorite programming language:  request.getParameter("favoriteLanguage") %> body> html>

Read HTML Form checkbox field value

student-checkbox-form.html

html> head> title>Student Registration Formtitle> head> body> form action="student-checkbox-response.jsp" method="post"> First name: input type="text" name="firstName" /> br/>br/> Last name: input type="text" name="lastName" /> br/>br/> input type="checkbox" name="favoriteLanguage" value="Java"> Java input type="checkbox" name="favoriteLanguage" value="C#"> C# input type="checkbox" name="favoriteLanguage" value="PHP"> PHP input type="checkbox" name="favoriteLanguage" value="Ruby"> Ruby br/>br/> input type="submit" value="Submit" /> form> body> html>

Deploy this page on tomcat server and hit respective URL in the browser will display below form on the browser:

student-checkbox-form.jsp

Let’s create a student-checkbox-response.jsp which will read above form data and display on a screen. Here is complete code in a page.

  1. We use request.getParameter() method to get the value of a form parameter.
  2. We can also use $ expression language to read the input value.
  3. We are using an expression tag to display a result.

Источник

Оцените статью