java left logo
java middle logo
java right logo
 

Home arrow Java Tutorials arrow Introduction to JSTL using NetBeans
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4101
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Introduction to JSTL using NetBeans E-mail
User Rating: / 163
PoorBest 

Create a new JSP file. Follow exactly the same way as we created the Jstl_Hello_World.jsp but name it Jstl_Core_Tags.jsp. Then we need to modify our index.jsp to contain the link to our newly created JSP. So please add below codes to our index.jsp just right after our first link to open the Hello World JSTL tags.

<tr>

<td>

<a href="Jstl_Core_Tags.jsp?valid=true&name=eric&mark=8">JSTL Core Tags</a>

</td>

<td>

JSTL if tag and EL operators

</td>

</tr>

What we do here is basically to send some parameters so that they are accessible from the target page.

On the target page, we can retrieve these parameters via ${param.parameter_name} and we are going to use the logical JSTL tags to compare some values. Add below codes to Jstl_Core_Tags.jsp

 

<c:if test="${param.valid}">

A test shows that parameter valid is ${param.valid}

</c:if>

This tag is used to determine whether parameter valid value is true or false. If the value is true then it prints out the sentence. On the other hand, if it is false, it should not print anthing out. You may like to try to modify the valid value to be false in the index.jsp. The sentence should not be printed out if the value is false. Until now, you should know that if you would like to access any parameters, you may use the keyword “param”. It is similar to the request.getParameter.

<c:if test="${param.name != null}">

A test shows that parameter name contains value of ${param.name} via != operator

</c:if>

The above tag is to check whether parameter name is null or not. If it is not null then it prints out the sentence and it prints nothing if the value is null.

<c:if test="${param.name ne null}">

A test shows that parameter name contains value of ${param.name} via ne operator

</c:if>

Above codes are working similarly with the previous codes. We just want to test JSP notation “ne” for not equal function as what “!=” does.

<c:if test="${not empty(param.name)}">

A test shows that parameter name contains value of ${param.name} via empty operator

</c:if>

Above codes are another feature of JSP notation to check a value whether it is empty or not. It is basically pretty much the same as the second and third tag do. The difference lies on the value null and empty. What is the difference between empty and null? Well, empty is not necessarily be null but null always be empty. Null means that the variable is simply not existed while empty means that the variable is existed and initialized but it contains nothing. Please be careful when dealing with null values as it may cause you the famous NullPointerException.

<c:if test="${param.mark >= 0 and param.mark <= 10}">

A test shows that parameter mark contains value of ${param.mark} which lies in range of 0 to 10

</c:if>

Above codes are used to check whether parameter with the name of “mark” is within the range of 0 to 10. If yes then it prints the sentence.

Again, press Shift+F11 to build the project and F6 to run. The index.jsp will now looks like this

 

Image

 

Try to click on the second link (JSTL Core Tags) and it should look like below screenshot.

 

Image

 

You may be interested to try out some different parameters values for this JSTL tag. Remember, you will learn a lot by practice. Let’s move forward to more advance JSTL core tags.

Create another JSP page. This time, we can name it with Jstl_Core_Tags_2.jsp. We also need to create another link in index.jsp to open up this page as well. Add below codes to create the additional new link in index.jsp.

<tr>

<td>

<c:url value="Jstl_Core_Tags_2.jsp" var="url">

<c:param name="test_for_each" value="one,two,three" />

<c:param name="test_for_token" value="one;two;three" />

</c:url>

<a href='<c:out value="${url}" />'>JSTL Core Tags 2</a>

</td>

<td>

Play around with JSTL forEach, forTokens, choose, when, and otherwise

</td>

</tr>

Now, we could see the new JSTL tag in index.jsp here. <c:param> is normally used to embed a parameter values to the URL link. While, <c:url> tag is basically used for creating a link of URL and stores the link in variable named “url”. In fact, this combination of <c:url> and <c:param> tags will do the same as the link that we hard coded before. The final link should be in form of Jstl_Core_Tags_2.jsp;jsessionid=…?param_1_name=param_1_value&param_2_name

=param_2_value. This is how we are creating a dynamic link using JSTL tag. After this, we can use <c:out> to display it.

In this case, we have 2 parameters named test_for_each and test_for_tokens. Next, we need to write JSTL forEach and JSTL forTokens tags in Jstl_Core_Tags_2.jsp to see how they work.

<c:forEach var="part_each" items="${param.test_for_each}">

<c:out value="${part_each}" />

<br>

</c:forEach>

Above codes will take values of the parameter “test_for_each” and stores it in variable called “part_each”. Within the tag forEach, we print the variable “part_each” using <c:out>. It loops through default separator ‘,’ (comma) until it reaches the end. <c:forEach> is extremely useful when you are sending some Collection object to the JSP and you need to iterate the content of the Collections to your JSP.

<c:forEach var="i" begin="1" end="20" step="2">

<c:choose>

<c:when test="${i < 11}"><c:out value="${i}" /> (small)<br></c:when>

<c:when test="${i < 16}"><c:out value="${i}" /> (medium)<br></c:when>

<c:otherwise><c:out value="${i}" /> (large)<br></c:otherwise>

</c:choose>

</c:forEach>

Above codes are the sample of more advanced forEach loop. We first declare a variable “i” and we also define the beginning, ending and stepping value for the variable “i”. The above tag means that it should loop from 1 to 20 in increment of 2. To make it more interesting, we could add a condition to check whether the current value of variable “i” is in the range of small, medium or large by using JSTL tag <c:choose>, <c:when> and <c:otherwise>. This one (<c:choose>, <c:when> and <c:otherwise>) is equivalent to switch case default syntax in conventional programming language.

<c:forTokens var="part_token" items="${param.test_for_token}" delims=";">

<c:out value="${part_token}" />

<br>

</c:forTokens>

This tag works in same way as forEach tag. But it has additional delims attribute to tokenize values with certain delimiters. What does it mean? It means that it will separate or tokenize the values within its items attribute with the delimiter. So if you have the String of “1,2,3” and the delimiter is “,”, the result would be three items i.e. 1, 2 and 3. How about if we want to separate the values for multiple delimiters? You can initialize it using multiple delimiters as delims=”;|()” to tokenize the values.

Let’s see how it works. Press Shift+F11 to build the project and F6 to run.

 

Image

 

And this is how the result looks like.

 

Image

 

Next, we are going to explain on <c:redirect> and <c:import>. These 2 tags are simple. <c:redirect> simply redirects us to another page and <c:import> is similar to <jsp:include> that will extend imported page to current page.



 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.