Overview of the Email List Application
The Email List application is a micro‑project that shows how a basic web interface can hand data over to a JavaServer Page (JSP) for processing. It is intentionally lightweight: one plain HTML file collects the user’s first name, last name, and email address; the JSP receives those three values, displays them back to the user, and offers a button to return to the form. Even though the business logic is minimal, the flow illustrates all the key pieces you need to master when you start building real JSP‑based web applications.
When you first create the project you’ll notice that the HTML and JSP live side by side in the same directory. This layout keeps the example simple, but it also mirrors a common pattern in small deployments: static pages that rely on JSPs for dynamic content. The HTML page contains a standard <form> element that submits its data via the HTTP GET method. The form’s action attribute points to show_email_entry.jsp, so the browser will issue a GET request for that JSP when the user clicks the submit button. Because GET requests encode the parameters directly in the query string, the JSP can retrieve them using the implicit request object.
Inside the JSP you’ll see a mix of ordinary HTML and Java code embedded between JSP tags. The Java snippets are responsible for extracting the three parameters, storing them in local variables, and then echoing those values back to the page with JSP expressions. The page ends with a simple form that contains a single “Return” button. That button uses a POST request to navigate back to the original form, allowing the user to try another entry. Although the example does not write anything to a database or file, you’ll later see how easy it is to add persistence logic by invoking a plain Java class from the JSP.
By working through this example, you’ll get a clear view of the following core concepts that every JSP developer needs to understand:
- How to design an HTML form that submits data to a JSP.
- How to capture request parameters in a JSP using scriptlets and expressions.
- How to use the implicit request object without explicitly creating it.
- How to send the user back to the original page with a form that triggers a POST request.
- How to structure a JSP so that the bulk of the page is readable HTML, with only small fragments of Java code.
These building blocks recur in almost every web application that mixes Java with HTML, so mastering them in this tiny example will pay dividends when you tackle more complex projects.
Building the HTML Form
The first step in creating the application is to write a straightforward HTML page that collects the user’s information. The form must contain three text fields - first name, last name, and email address - and a submit button. It should also include a clear headline and brief instructions so that visitors know what to do. Below is the complete source for
join_email_list.htmlThe page starts with the classic HTML 4.01 transitional doctype. Although modern projects might use HTML5, keeping the doctype simple prevents the browser from injecting quirks that could interfere with the JSP’s ability to read the parameters. Inside the
<head>we set the title so the browser’s tab displays “Chapter 4 – Email List application”. The body contains an<h1>heading, a<p>paragraph with instructions, and the form itself.When constructing the form, pay attention to the
actionattribute. It must point to the exact filename of the JSP that will process the input,show_email_entry.jsp, and it should live in the same directory as the HTML file. Themethodattribute is set togetbecause GET requests are convenient for debugging: the submitted values appear in the URL and are easy to inspect with browser tools.Inside the form we use a table for layout. Each row contains a label in the left cell and an
<input>element in the right cell. Thenameattribute of each input is critical because it becomes the key that the JSP uses to retrieve the submitted value. In this example, the names arefirstName,lastName, andemailAddress. When the user submits the form, the browser will send a request like:GET /show_email_entry.jsp?firstName=John&lastName=Smith&emailAddress=jsmith%40hotmail.com HTTP/1.1The JSP will then pull each parameter out using
request.getParameter("…"). After the table, the form ends with a single submit button that says “Submit”. Because the form uses the GET method, the button’s default action is to reload the page with the query string. The browser automatically appends the parameter values to the URL, so the JSP has everything it needs to process the input.Here is the full HTML source again for reference:
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"></p> <p><html></p> <p><head></p> <p> <title>Chapter 4 - Email List application</title></p> <p></head></p> <p><body></p> <p> <h1>Join our email list</h1></p> <p> <p>To join our email list, enter your name and</p> <p> email address below. <br></p> <p> Then, click on the Submit button.</p></p> <p> <form action="show_email_entry.jsp" method="get"></p> <p> <table cellspacing="5" border="0"></p> <p> <tr></p> <p> <td align="right">First name:</td></p> <p> <td><input type="text" name="firstName"></td></p> <p> </tr></p> <p> <tr></p> <p> <td align="right">Last name:</td></p> <p> <td><input type="text" name="lastName"></td></p> <p> </tr></p> <p> <tr></p> <p> <td align="right">Email address:</td></p> <p> <td><input type="text" name="emailAddress"></td></p> <p> </tr></p> <p> <tr></p> <p> <td></td></p> <p> <td><br><input type="submit" value="Submit"></td></p> <p> </tr></p> <p> </table></p> <p> </form></p> <p></body></p> <p></html>With the form ready, you can launch the web container (e.g., Apache Tomcat), open
join_email_list.htmlin a browser, and verify that the page loads correctly. The next step is to create the JSP that receives the data and displays it back to the user.Crafting the JSP to Handle Data
The heart of the application is
show_email_entry.jsp. Although the page looks like a plain HTML document, it contains embedded Java code that interacts with the incoming request. The JSP follows a simple pattern: read the parameters, assign them to local variables, and then embed the values in the HTML output using expressions.At the very top of the file, right after the doctype and
<html>tag, the JSP places a scriptlet. Scriptlets are sections of Java code wrapped in<% … %>tags. Inside this scriptlet we callrequest.getParameter()three times, once for each field submitted by the form. Because the JSP runs on the server side, the implicitrequestobject is automatically available without needing to instantiate it. The resulting values are stored in threeStringvariables:firstName,lastName, andemailAddressAfter the scriptlet, the page continues with standard HTML: a heading that thanks the user for joining the list, a paragraph that introduces the table, and then the table itself. Each table cell that displays a value uses a JSP expression, which is a shorthand for printing a string to the output stream. Expressions are surrounded by
<%= … %>tags. For instance,<%= firstName %>will output the content of thefirstNamevariable directly into the HTML. This keeps the code readable while still allowing dynamic content.At the bottom of the page, after the table, we add another form that contains a single “Return” button. This form’s
actionpoints back to the originaljoin_email_list.htmlfile, and its method is POST. Although the form uses POST, no data is actually sent; the button simply triggers a new request to the HTML page, which allows the user to enter another set of values. In a more sophisticated application, you might replace this button with a link or a redirect, but the form approach demonstrates how to combine JSP and standard HTML forms.Below is the full source for
show_email_entry.jsp:<html>
<head>
</head>
<body>
<%
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");
%>
<h1>Thanks for joining our email list</h1>
<p>Here is the information that you entered:</p>
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td><%= firstName %></td>
</tr>
<tr>
<td><%= lastName %></td>
</tr>
<tr>
<td><%= emailAddress %></td>
</tr>
</table>
<p>To enter another email address, click on the Back <br>
button in your browser or the Return button shown <br>
below.</p>
<form action="join_email_list.html" method="post">
<input type="submit" value="Return">
</form>
</body>
</html>
When you deploy this JSP in a servlet container, the server compiles it into a Java servlet behind the scenes. The scriptlet becomes a block of Java code inside the servlet’s
service()method, while the expressions translate intoout.write()calls that stream the resulting HTML to the client. Although you don’t write those compiled classes yourself, understanding the transformation helps when you debug or optimize a JSP.Running the application now will show a two‑page flow: the form, the JSP confirmation page, and the ability to return to the form. You can test with various inputs, including email addresses that contain special characters, to see how the GET query string handles them. This test will confirm that the
request.getParameter()calls are retrieving the data correctly.Enhancing the Application with Persistence
Up to this point, the application merely echoes user input back to the browser. The next logical step is to make the data survive beyond the request by writing it to a file or database. This demonstrates how to blend pure JSP code with regular Java classes that handle business logic and data storage.
The most straightforward approach is to create a plain Java bean that represents a subscriber record. A bean is just a simple class with private fields, a no‑argument constructor, and getter and setter methods. For the email list example, a
Subscriberclass would have three fields:firstName,lastName, andemailAddress. By exposing these fields through getters and setters, the class becomes a convenient carrier for the data between the JSP and the persistence layer.Next, add a second Java class,
SubscriberDao, that handles writing aSubscriberobject to a file. The DAO (Data Access Object) pattern encapsulates the details of file I/O, allowing the JSP to remain clean and focused on presentation. The DAO’ssavemethod might open aBufferedWriterin append mode, format the subscriber data as CSV, and write the line to a text file. To keep the example portable, the file could live under the web application’sWEB-INFdirectory, which is not directly accessible from the browser.Inside the JSP, replace the raw scriptlet that assigns the request parameters with a call to the DAO. First, construct a
Subscriberobject by passing the request parameters to its constructor or by calling the setters. Then create aSubscriberDaoinstance and invokesave(subscriber). The DAO writes the data to the file, and the JSP can still display the values to the user in the same table format.Here is a concise illustration of what the updated scriptlet might look like:
<%@ page import="com.example.Subscriber, com.example.SubscriberDao" %> <%</p> <p> Subscriber subscriber = new Subscriber(firstName, lastName, emailAddress);</p> <p> SubscriberDao dao = new SubscriberDao();</p> <p> dao.save(subscriber);</p> <p>%>With this change, every time a user submits the form, the data is appended to
subscribers.txt, and the confirmation page still shows the entered values. If you open the file in a text editor or read it back into the application, you will see a simple CSV list of all subscribers. This demonstrates how easily you can extend a JSP‑based application with Java classes that perform business logic and persistence.From here, the next learning step could be to replace the flat file with a relational database such as MySQL or PostgreSQL. You would write a JDBC DAO that connects to the database, prepares an
INSERTstatement, and executes it. The rest of the JSP remains unchanged, illustrating the power of separating concerns: the JSP handles presentation, while the Java classes manage data and business rules.Through this incremental approach - starting with a static form, moving to a JSP that processes parameters, and finally adding a persistence layer - you gain a solid grasp of how JavaServer Pages fit into the larger servlet ecosystem. Mastering these fundamentals will prepare you for building more sophisticated web applications that rely on dynamic content, user authentication, session management, and database integration.





No comments yet. Be the first to comment!