In the final part of the tutorial, we will integrate all we have learned so far to showcase the best practice used when working with servlets and JSPs. Previous chapters have shown strengths and weakness of both servlets and JSP, and this chapter will demonstrate the best use of both.
As we have seen, servlets are good for implementing business logic and control flow, but are not the best choice for generating HTML output, especially when it is complex. On the other hand, JSPs make display very easy, but we should avoid cluttering it with business logic code.
The way it is usually done is that JSPs are used to accept user input and generate results of the processing, while servlets are used for processing and navigation. This is what we will do for this chapter's sample.
We will create simple form page that will submit some data. Data will be processed by the servlet, and then servlet will forward to another JSP page, which will display results of the processing.
Form Page
Our form page will be quite simple. All it contains are two fields in which user can enter it's name and age, and submit button. When user submits the form, it will be taken to a page, which will display results of the computation.
Source code for the form page is shown below:
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
<jsp:directive.page contentType="text/html" pageEncoding="UTF-8"/>
<jsp:element name="form">
<jsp:attribute name="action">/process</jsp:attribute>
<jsp:attribute name="method">post</jsp:attribute>
<div>
Name: <input type="text" name="name" size="30" />
Age: <input type="text" name="age" size="5" />
<input type="submit" value="Submit" />
</div>
</jsp:element>
</jsp:root>
Result page
The result page will display results of the computation, in this case the year in which user is born. This is trivial example, but will serve good to show the basic principles of servlet-JSP integration. Source code for result page is shown below:
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
<jsp:directive.page contentType="text/html" pageEncoding="UTF-8"/>
<jsp:element name="div">
<jsp:body>
Your name is ${username}. You were born in ${year}.
</jsp:body>
</jsp:element>
</jsp:root>
This is the simplest possible page, which will only display user's name and computed year of birth. The request attribute for name must be “username”, and attribute for year of birth must be “year”.
Processing Servlet
The servlet will contain business logic for this operation, i.e. calculation of user's year of birth. Servlet's code is displayed below:
public class ProcessServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
String ageString = req.getParameter("age");
// find current year
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int birthYear = year - Integer.parseInt(ageString);
// set calculated values and forward to results page
req.setAttribute("username", name);
req.setAttribute("year", birthYear);
req.getRequestDispatcher("/results.jspx").forward(req, resp);
}
}
First, we pick up information user provided as request parameters. Then we calculate user's year of birth based on information provided. In the final step, we set computed information as request attribute, and forward user to results page.
Note the starting “/” in “/results.jspx”. This means that resource is resolved relative to context root.
Finally, we need to set up new servlet in deployment descriptor:
<servlet>
<servlet-name>processServlet</servlet-name>
<servlet-class>com.beyondrelational.ProcessServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>processServlet</servlet-name>
<url-pattern>/process</url-pattern>
</servlet-mapping>
As the final result, we get the result page similar to the following:

Find the sample web application here