JSP & HTML Forms – 1

This entry is part 7 of 14 in the series JSP Course

All of us, at one point or another, have interacted with HTML forms on websites. Whether you sign up/in on websites, or you make reservations through an agency’s portal, you are putting information into HTML forms. The following is an example HTML form.

 1

So what we are going to do here is to build an HTML form to read student information. We are going to prompt the student for their first name and their last name, they are going to enter it into a text field and once they are done with it, they’ll click on the submit button. The JSP will read this information and then send back a confirmation page to the browser.

Let’s get on to building the form and see how to fetch data from it with JSP.

Building the HTML Form

By this point you know the basic markup for HTML. So make a page and add the following markup to construct a form:

2

Explanation

So when you start an HTML form, you start with the form tag and with the action you are basically telling the form to send the data to student-response.js. Next we’ve set up our text fields i.e. First Name & Last Name and named them respectively. Then we have set up a submit type input for the “Submit” button. The value attribute of the input tag is just the label you’ll be seeing on the button.

Now when you’ll click the Submit button, you’ll be sending the data from this form to the JSP Page.

Building the JSP Page

In order to read the data from the HTML form you need to know the names of the fields in the HTML form. Using the form, we built above, here is the code for the JSP File.

The student is confirmed:

Explanation

So we read the form data by using JSP expressions and calling the request object to get Parameter and then we reference it with the name of the field, wherever needed.

Alternatively, you can do this with the following code:

The following student is confirmed: ${param.firstName} ${param.lastName}

This syntax is used majorly for reading and displaying form data.

Using Drop Down Lists

For this we are going to use the form we have constructed above and add just a drop down list to it, then extract data from it using JSP.

3

The form

The JSP

The following student is confirmed: ${param.firstName} ${param.lastName}

We add to it:

The student’s country: ${param.country}

Just a reminder this method is just for displaying data, if you want to use this information in scriptlets, you’ll have to use “request.GetParameter()”.

That’s all for this tutorial. In the next tutorial we are going to explore more options to fetch data from HTML forms.

Series Navigation<< JSP implicit objectsJSP and HTML Forms – 2 >>

Leave A Comment

Your email address will not be published. Required fields are marked *