Community Sites
Create your own community website and start earning today !
It's Free !
 
Communities Members BookmarksPolls Fresher Jobs Funny Pictures MCA Projects New Member FAQ  



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



JSTL: Getting Started - PART1


Posted Date: 20 Mar 2008    Resource Type: Articles/Knowledge Sharing    Category: Placement Papers

Posted By: ramya       Member Level: Gold
Rating:     Points: 5





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.

Don't Repeat Yourself, also known as the DRY principle, is a philosophy that reduces development time and increases the efficiency of a team working on a web application. Scripting languages such as PHP, Ruby based RoR, and so on, have this idea as part of their core architectural design. But for compiled languages such as Java (say servlets), it was not part of their core, especially for the View aspect.

However, this began to change for Java when JSP was brought to the fore. JSP provided all the required objects as implicit objects, including session and request related objects. Even though the implicit objects reduced the development time, the development of the View aspect was more or less based on reinventing the wheel.

The code implementing iteration and branching was this kind of code. This was the situation until custom tags were introduced. After their introduction, many of the daily routine codes, such as branching and looping over varied collections, made their appearance as custom tags. The standardized set of such custom tags is known as the JSP Standard Tag Library, or JSTL for short.

In this discussion I will be focusing on the basics of JSTL. The first section will focus on the terminology of the JSTL. The sets of tags within JSTL will be the focus of the second section. In the third section I will develop an application that will provide a simple interface for displaying retrieved data using the JSTL. That sets the theme for this discussion.

Understanding the Terminology



Each new technology brings a host of new terminology with it. JSTL is no exception. Having custom tags as its base, JSTL has eased the terminology aspect a bit, yet there are new additions to the vocabulary of custom tags. These additions can be broadly divided into two groups, which are based on Expression Language and Tag Attributes. The Expression Language related terminology deals with how to manipulate the data at View, whereas the Tag Attribute related terms deal with passing the data to the tag for presentation.

Expression Language

Expression language or EL is the single most important feature of JSTL because it reduces the need to specify tag attribute values with scriptlets. In addition to this, EL provides a universal approach for accessing all sorts of application data including beans, arrays, lists, maps, cookies, request parameters and so on. The services that essentially EL provides are:
Expression and identifiers
Arithmetic, logical and relational operators
Automatic type coercion
Universal data access approach

Of these, the first two are only syntactically different from what we have been doing in scriptlets, whereas the last two are new to EL.
Expression and identifiers. In EL expressions are invoked using the ${expr}syntax, where the expression to be invoked and evaluated is represented by expr. To make this more clear, let me contrast between the EL and scriptlet way of passing values to the tag attributes. In scriptlets the only way to pass data to the attribute involves using Java code. For example, if the value of a string variable tr has to be passed to the attribute value of a tag out with the prefix ex, the syntax would be:



This in turn means that even if custom tags try to reduce the Java code within JSP, passing the value in such a way undermines the philosophy behind custom tags. The same thing can be done in a much easier way using EL thus:



In EL identifiers are the name of objects stored in scope, where scope can be page, request, session or application. The default scope is request. In the above example str is an identifier.
Arithmetic, logical and relational operators. In the case of these operators EL has made no changes. As with scriptlets, the expression can contain logical, arithmetic or relational operators. So the following EL expression is completely valid:


Automatic type coercion. To change one type of data into another, explicit typecasting must be done in scriptlets. For example, if the attribute expects an int value and the data to be passed is a string value, then the code would be:



Whereas in EL it would be:



Here EL does the type conversion implicitly with the developer not required to keep tabs on it. The most dreaded exception for any programmer in the case of a tag is a null value exception. With EL this dread need not exist because EL converts the null strings into empty strings and null values into 0.
Universal data access approach. Accessing data is one of the tough jobs for a Java programmer due to the fact that different members of the collection framework requires different types of access mechanisms. List based classes use index based access, whereas map based classes use key based access. To make the data access universal, EL has provided a bean based access mechanism along with all other types. The bean based access makes it possible to access collections just as one would access a bean's properties. This access is even extended to headers, request parameters and cookies. For example, to access a parameter named lastName from the request scope the EL would be:



Here the term param is a keyword representing the parameters passed as a part of request.

Tag Attributes

JSTL shares many commonalities with the HTML tags. Apart from the syntactical similarity, the attributes share the same name and functionality across different tags. The following list includes the attributes that are common in names and functionalities:
value: This attribute takes the value that must either be displayed or passed on to another tag by using the var attribute.
var: When the passed value must be set in scope or used by another tag, this attribute comes into the picture. The value passed to this attribute is used as a part of an expression in the next tag.
items: This attributes comes in handy when working with collections. It takes the name of the collection. The name can be the key of the parameter map or the key of the key/value pair set in the session.

These are the three main attributes that are used by different tags for the same purpose.

So that covers the terminology regularly used in JSTL. In the next section I will be discussing the various tags available as part of JSTL.

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.

Don't Repeat Yourself, also known as the DRY principle, is a philosophy that reduces development time and increases the efficiency of a team working on a web application. Scripting languages such as PHP, Ruby based RoR, and so on, have this idea as part of their core architectural design. But for compiled languages such as Java (say servlets), it was not part of their core, especially for the View aspect.

However, this began to change for Java when JSP was brought to the fore. JSP provided all the required objects as implicit objects, including session and request related objects. Even though the implicit objects reduced the development time, the development of the View aspect was more or less based on reinventing the wheel.

The code implementing iteration and branching was this kind of code. This was the situation until custom tags were introduced. After their introduction, many of the daily routine codes, such as branching and looping over varied collections, made their appearance as custom tags. The standardized set of such custom tags is known as the JSP Standard Tag Library, or JSTL for short.

In this discussion I will be focusing on the basics of JSTL. The first section will focus on the terminology of the JSTL. The sets of tags within JSTL will be the focus of the second section. In the third section I will develop an application that will provide a simple interface for displaying retrieved data using the JSTL. That sets the theme for this discussion.

Understanding the Terminology



Each new technology brings a host of new terminology with it. JSTL is no exception. Having custom tags as its base, JSTL has eased the terminology aspect a bit, yet there are new additions to the vocabulary of custom tags. These additions can be broadly divided into two groups, which are based on Expression Language and Tag Attributes. The Expression Language related terminology deals with how to manipulate the data at View, whereas the Tag Attribute related terms deal with passing the data to the tag for presentation.

Expression Language

Expression language or EL is the single most important feature of JSTL because it reduces the need to specify tag attribute values with scriptlets. In addition to this, EL provides a universal approach for accessing all sorts of application data including beans, arrays, lists, maps, cookies, request parameters and so on. The services that essentially EL provides are:
Expression and identifiers
Arithmetic, logical and relational operators
Automatic type coercion
Universal data access approach

Of these, the first two are only syntactically different from what we have been doing in scriptlets, whereas the last two are new to EL.
Expression and identifiers. In EL expressions are invoked using the ${expr}syntax, where the expression to be invoked and evaluated is represented by expr. To make this more clear, let me contrast between the EL and scriptlet way of passing values to the tag attributes. In scriptlets the only way to pass data to the attribute involves using Java code. For example, if the value of a string variable tr has to be passed to the attribute value of a tag out with the prefix ex, the syntax would be:



This in turn means that even if custom tags try to reduce the Java code within JSP, passing the value in such a way undermines the philosophy behind custom tags. The same thing can be done in a much easier way using EL thus:



In EL identifiers are the name of objects stored in scope, where scope can be page, request, session or application. The default scope is request. In the above example str is an identifier.
Arithmetic, logical and relational operators. In the case of these operators EL has made no changes. As with scriptlets, the expression can contain logical, arithmetic or relational operators. So the following EL expression is completely valid:


Automatic type coercion. To change one type of data into another, explicit typecasting must be done in scriptlets. For example, if the attribute expects an int value and the data to be passed is a string value, then the code would be:



Whereas in EL it would be:



Here EL does the type conversion implicitly with the developer not required to keep tabs on it. The most dreaded exception for any programmer in the case of a tag is a null value exception. With EL this dread need not exist because EL converts the null strings into empty strings and null values into 0.
Universal data access approach. Accessing data is one of the tough jobs for a Java programmer due to the fact that different members of the collection framework requires different types of access mechanisms. List based classes use index based access, whereas map based classes use key based access. To make the data access universal, EL has provided a bean based access mechanism along with all other types. The bean based access makes it possible to access collections just as one would access a bean's properties. This access is even extended to headers, request parameters and cookies. For example, to access a parameter named lastName from the request scope the EL would be:



Here the term param is a keyword representing the parameters passed as a part of request.

Tag Attributes

JSTL shares many commonalities with the HTML tags. Apart from the syntactical similarity, the attributes share the same name and functionality across different tags. The following list includes the attributes that are common in names and functionalities:
value: This attribute takes the value that must either be displayed or passed on to another tag by using the var attribute.
var: When the passed value must be set in scope or used by another tag, this attribute comes into the picture. The value passed to this attribute is used as a part of an expression in the next tag.
items: This attributes comes in handy when working with collections. It takes the name of the collection. The name can be the key of the parameter map or the key of the key/value pair set in the session.

These are the three main attributes that are used by different tags for the same purpose.

So that covers the terminology regularly used in JSTL. In the next section I will be discussing the various tags available as part of JSTL.





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: J2EE ESSENTIALS - SUMMARY
Previous Resource: interview questions
Return to Discussion Resource Index
Post New Resource
Category: Placement Papers


Post resources and earn money!
 
Related Resources



Watch TV Channels
  • Watch Asianet TV online
  • Kairali TV in Internet
  • Surya TV online
  • Amritha TV Channel

  • Contact Us    Privacy Policy    Terms Of Use   

    SpiderWorks Technologies Pvt Ltd. 2006 - 2007 All Rights Reserved.