JSTL: Getting Started - JSTL in the Real World
(Page 4 of 4 )
The application to be developed contains three major components:
- sRetrieveData, sGetEmpData: These are servlets that retrieve the data and set it into the request scope. Of these, only one of the servlets' code will be discussed because the other is more or less similar.
- ShowDetails.jsp: This is the JSP page that will display the data.
- EmpBean: This is the JavaBean containing the employee details.
So here is the first servlet that retrieves the employee ids:
package com.someorg.servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.PrintWriter;
import java.io.IOException;
public class sRetrieveData extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html;
charset=windows-1252";
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType(CONTENT_TYPE);
doPost(request,response);
//get the employee ids from database.
//Connection code not shown for brevity
ArrayList list=new ArrayList();// List to hold the employee ids
while(rs.next())//rs is the resultset containing the employee ids
{
list.add(rs.getString(1));
}
request.setAttribute("LIST",list);//set the list in request scope
request.getRequestDispatcher("/ShowDetails.jsp")
.forward(request,response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
}
}
The code in bold performs the retrieval and sending of the data to the next page. Next, let's look at the JSP. I will show only the part where the JSTL is being used.
<table cellspacing="2" cellpadding="3" border="1" width="88%">
<tbody>
<tr>
<th width="56%">
Employee Id
</th>
<th width="44%">
<select size="1" onChange="submitForm();">
<c:forEach items="LIST" var="item">
<option value="<c:out value="${item}"/>">
<c:out value="${item}"/>
</option>
</c:forEach>
</select>
</th>
</tr>
</tbody>
</table>
The foreach iterates over the list set in the request scope and passes the value to out using the var attribute. Out then displays it. This page shows both the ids and the details on the basis of selected id. Unless the id is selected, the details should not be shown. For this, a simple logic can be applied. The details will be contained within a list. So if id is not selected, the list will be empty. Hence by checking for the list, the details part can be either shown or hidden. Here is the code:
<c:if test="not empty DETAILS">
<table cellspacing="3" cellpadding="2" border="1" width="776%">
<tbody>
<tr>
<th width="66%">
Emp Name
</th>
<th width="21%">
Emp Salary
</th>
<th width="14%">
Hire Date
</th>
</tr>
<c:forEach items="DETAILS" var="emp">
<tr>
<td width="66%">
<c:out value="${emp.empName}"/>
</td>
<td width="21%">
<c:out value="${emp.empSal}"/>
</td>
<td width="14%">
<fmt:formatDate value="${emp.hireDate}" format="MM-dd-yyyy"/>
</td>
</tr>
</tbody>
</table>
</c:if>
On selecting the id, the second servlet is called, which then populates the data and sets it in the list, which is then passed to the JSP using the request scope (the servlet and the bean are not shown for the sake of brevity). Once the data reaches the JSP page and the if condition is satisfied, the data is iterated over and displayed. Since I want to display the hire date in a specific format, I have used the formatDate tag. That's it. It's very simple, and no scriptlet is used.
In the above code there are certain architectural loopholes. Rectifying these using the JSTL and the remaining JSTL tags will be the topic of the next article. Till then...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |