Employee Form

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.

Читайте также:  Python add argument type

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.

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.

Источник

Radio Button in JSP Page Example

Hey guys in this article, you will learn about creating the radio button and reading the radio button value in JSP with full code example.

  • Check the Complete JSP Tutorials
  • Check the Complete Spring Boot Tutorials [100+ Examples]
  • Check the Complete Spring Boot and Thymeleaf Tutorial
  • Check the Complete AWS Tutorial
  • Check the Complete JavaServer Faces (JSF) Tutorial
  • Check the Complete Spring Data JPA Tutorial
  • Check the Complete Spring Security Tutorial
  • Check the Javascript Projects for Beginners
  • Check the Spring Boot JdbcTemplate Tutorials

Overview

To create a radio button in HTML we will use tag. It contains name attribute.

To read the radio button value in JSP, we will use the request object

getParameter() takes the name of the radio button. Another shortcut method to read radio button value is using param object

Using param object we can read the radio button value.

Complete Example

Follow the below steps to understand the complete example

Create a Project

You can check the following post to create a new Dynamic Web Project

Create a HTML form

Create EmployeeForm.html file under WebContent and add the following content

      
First Name:

Last Name:

Select country:

Select Gender: Male Female

Create a JSP file

Create employee-details.jsp file under WebContent and add the following content

       

Employee details

First Name: $

Last Name: $

Country: $

Gender: $

Run the file

Right click on the File, choose Run As, choose Run on Server. It will open in a default web browser, you will see the following content
9

10

That’s it for this post, if you like this post, please share this post with your friends and collogues and also share this on your social media profiles.

About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Источник

How to use radio button in jsp page

This tutorial is about how to use radio button in jsp page.

How to use radio button in jsp page

How to use radio button in jsp page

This is detailed java code how to use radio button in jsp code and display a message in another jsp page according to selected radio buttons. First page in this example lets the user enter its name and select stream, branch and course type. On submission of the request, entered information is shown in the next page.

        
Student Name Stream B.Tech. MCA MBA Branch CS IT EC Course Type Regular Correspondance Online

Save this code as a .jsp file named » first_page.jsp » in your application directory (‘user’, in our case) in Tomcat.

       Congratulations ! 

Welcome .

Stream Branch Course Type edit

Save this code as a .jsp file named » show_data .jsp » in your application directory (‘user’, in our case) in Tomcat.

To start Tomcat server, run startup.bat file from directory Tomcat-6.0.16/bin/startup.bat. To run this application open browser and type http://localhost:8080/user/first_page.jsp in address bar and hit enter key.

Fill Student name (For example, Mahendra Singh) and select stream, branch, course type according to student data then click submit button that will call another jsp page named » show_data.jsp», shows information of student that has been selected in first page.

To change or edit displayed information, click on link «edit».

Tutorials

  1. Creating a String
  2. Introduction to JSP
  3. Reading Request Information
  4. Introduction to the JSP Java Server Pages
  5. JSP FUNDAMENTALS
  6. Using Taglib in JSP. A brief introduction to taglibs and taglibs programing.
  7. JSP date example
  8. Reading Request Information
  9. Using Beans in JSP. A brief introduction to JSP and Java Beans.
  10. JSP 2.0 — New Features
  11. JSP Excel Tutorial
  12. Create Excel Sheet Using JSP
  13. Inserting Image In Excel Sheet Using JSP
  14. How to Create Excel Page Using JSP
  15. How to Create New Excel Sheet Using JSP
  16. Create Excel Sheet Using JSP
  17. Passing Various Data Types into Cells
  18. Assing Date into Cells
  19. Reading And Writing Excel File
  20. Selecting Excel Sheet File
  21. Zoom in Excel
  22. Zoom Out Excel
  23. Merging Two Cells
  24. Working With Fonts
  25. Working With Alignment Using JSP for Excel
  26. Shifting Row Using JSP
  27. Freeze And Split Pane
  28. Repeating Rows And Column
  29. different borders
  30. Create Shape in Excel Using JSP
  31. Creating Oval in Excel Using JSP
  32. Create Box in Excel Sheet Using JSP
  33. Filling Color in Excel Using JSP
  34. Set Font into TextBox String Using JSP
  35. Fit Hight Ad Wdth of Ecel Seet Uing JSP
  36. Setting Line Style
  37. Finding a Factorial using while loop
  38. JSP Simple Examples
  39. Calculate factorial Using Recursion
  40. Multiple Methods in Jsp

Источник

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