Home arrow JavaScript arrow Page 4 - JSTL: Getting Started
JAVASCRIPT

JSTL: Getting Started


The JSP Standard Tag Library, or JSTL for short, is a standardized set of custom tags used in Java that can save programmers a lot of time and trouble. This article explores the basics of the JSTL.

Author Info:
By: A.P.Rajshekhar
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
June 20, 2006
TABLE OF CONTENTS:
  1. · JSTL: Getting Started
  2. · JSTL: Understanding the Terminology
  3. · JSTL: Understanding the Types
  4. · JSTL in the Real World

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
JSTL: Getting Started - JSTL in the Real World
(Page 4 of 4 )

The application to be developed contains three major components:

  1. 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.
  2. ShowDetails.jsp: This is the JSP page that will display the data.
  3. 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.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials