JSP: Grab Form Data Using getparamter

Introduction to using the Java getparamter() to grab data from an HTML form.

If you have ever created some sort of online form and processed the data before in any other language (PHP, Javascript, Python) you should take comfort in knowing it is done the same exact way when using Java and JSP. The main difference is syntax. It’s actually the only difference.

1. I have two files for this tutorial. The index.jsp file which is my html form. Then a process.jsp file that will process the form. Let’s take a look at our index.jsp file first below:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Web Form 200005</title>
    </head>
    <body>
        <h1>Fill out the form below to sign up for our mailing list!</h1>
        
        <form action="process.jsp" method="post">
            Name: <input type="text" name="fName"></input>
            Email: <input type="text" name="eMail"></input>
            <input type="submit" value="submit" />
        </form>
    </body>
</html>

What we want to focus on is the form tag and everything in it. Notice our form has an action= attribute. This is an HTML element that specifies where to send the form data when a submit button is clicked. It links to a specific file. In our case, that file is process.jsp. You can use a relative file name path or a direct url for where to find the file.

Next, we use a value of post for the method attribute in the form tag. This has two possible values: get or post. By default if not specified, your form will use a get method. This sends data openly in the url of your form which is bad for forms with important data. We use the post method which is more secure.

2. Each form element in your HTML form tag must have a name=” “ attribute or else you can not target that specific element. Simply put in name attributes for each element


<input type="text" name="fName">
<input type="text" name="eMail">

3. When our form is filled out and someone clicks submit, it will send the form data to the file process.jsp. In our process.jsp file it will also display the content of it. Therefore, we want to have a message that tells the user they submitted their info correctly. We also do the heavy lifting on this page. Let’s look it it bare first:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Yay!</title>
    </head>
    <body>
        <h1>Hello, thanks for signing up, we got your info!</h1>
    </body>
</html>

Very simple page that displays a thank you message. Now let’s do some heavy lifting.

4. At the top of our body we are going to insert 2 lines of code (refered to as a scriptlet) that will do 2 things: Grab the value from the specified HTML form element, and then store that value in a new string variable.


<%
String firstname = request.getParameter("fName");
String email = request.getParameter("eMail");
%>

First we declare a new variable. Next, we set it equal to the parameter in the “fName” and “eMail” fields from our HTML form.

5. The method we used to grab the data was getParamter() – which again, simply gets the value of a form parameter. They’re several other methods for getting data from other types of form elements (such as listboxes and checkboxes). The request.getParameter() part of the method tells the method to get the parameter from the implicit request object. This is an object that is automatically created in the JSP container for each individual page, containing data from the prior. It is often refereed to as the implicit request object.

6.Let us take our newly acquired data and spit it back out to the user:


<h1>Hello, <%= firstname %> - we added <%= email %> to our list. Thanks!</h1>

We use a simple JSP expression to echo out our variables content. We convert the values to a string perfect for spitting back out at the user.

The full process.jsp page:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Yay!</title>
    </head>
    <body>
        <%
            String firstname = request.getParameter("fName");
            String email = request.getParameter("eMail");
        %>
        
        <h1>Hello, <%= firstname %> - we added <%= email %> to our list. Thanks!</h1>
    </body>
</html>

The full index.jsp page:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Web Form 200005</title>
    </head>
    <body>
        <h1>Fill out the form below to sign up for our mailing list!</h1>
        
        <form action="process.jsp" method="post">
            Name: <input type="text" name="fName"></input>
            Email: <input type="text" name="eMail"></input>
            <input type="submit" value="submit" />
        </form>
    </body>
</html>

 

About Chris Luongo

Chris is a self-taught web designer and developer out of Atlanta, GA. This geek enjoys coffee, cold brews, bike riding, and twisting any form of tech into submission.
View more articles by Chris Luongo

The Conversation

Follow the reactions below and share your own thoughts.

Leave a Reply