JavaScript
  Home arrow JavaScript arrow Page 4 - JSTL: Getting Started
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

JSTL: Getting Started
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-06-20

    Table of Contents:
  • JSTL: Getting Started
  • JSTL: Understanding the Terminology
  • JSTL: Understanding the Types
  • JSTL in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

       · Hi allJSTL provides HTML like tags for almost all the common routines in Java. In...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT