Professional Documents
Culture Documents
<%@ page contentType="text/html" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <html> <body bgcolor=lightblue> <form method=post action="demo1.jsp"> NAME <input type=text name="text1"><br> PLACE<input type=text name="text2"><br> <input type=submit> </form> NAME:<c:out value="${param.text1}" /><br> PLACE:<c:out value="${param.text2}" /> </body> </html> ---------------------------------------------In all the previous examples, we invoked the JSP file through a html file. But, in demo1.jsp, we are posting the page to itself.( in asp.net style!).( but there is no 'retention of data' , unlike asp.net). We start Tomcat5, and type the url as : ?http://localhost:8080/demo1.jsp?. in the browser.We get a form with two text boxes and a submit button. We fill up the textboxes with ?name? and ?place? and submit. The demo1.jsp executes and displays the values entered by the user.due to the JSTL tags <c:out value=?${param.text1} /> etc.
That is about our first and introductory example. ----------- --The second example is very important. When the user enters data in a number of fields, it is tedious to collect the data and transfer it to jsp page for processing. In our example, we are collecting data about a player, such as his name, place and game. We can have much more but we are restricting for space considerations. JSP has an action tag , known as 'jsp:setProperty'. Using this along with a standard javabean, we can extract data and transfer it to our program in a single step.
In demo2.jsp, we collect the data and then display the data entered by the user. Note that instead of {param.text1}, we are using {bean1.name}. We should carefully name the html form controls with the corresponding attribute names given in the bean. We cannot name the controls as 'text1' etc, now! <c:out <c:out <c:out --We get correct result. ============================================= // demo2.jsp value="${bean1.name}" /> value="${bean1.place}" /> value="${bean1.game}" />
<%@ page contentType="text/html" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <html> <body> <form method=post action="demo2.jsp"> <jsp:useBean id="bean1" class="ourbeans.player"> <jsp:setProperty name="bean1" property="*" /> </jsp:useBean> Name <input type=text name="name"><br> Place<input type=text name="place"><br> Game<input type=text name="game"><br> <input type=submit> </form> Name: <c:out value="${bean1.name}" /><br> Place: <c:out value="${bean1.place}" /><br> Game: <c:out value="${bean1.game}" /> </body> </html> ============================================= Once again, it will be noticed that there is no java code in this example, as everything is being done by tags, only.. *********************************************** We are now ready to take up examples for 'condition' tags. There are two types of 'condition tags'. namely, <c:if> & <c:choose>.
In the third demo, we learn how to use the <c:if tag. ----------------------------------------------
JSTL Tutorial Part 2 // demo4.jsp <%@ page contentType="text/html" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> Example <html> <body bgcolor=lightblue> <form method=post action="demo3.jsp"> <select name="combo1"> <option value="1">1 </option> <option value="2">2 </option> <option value="3">3 </option> <option value="4">4 </option> <option value="5">5 </option> <option value="7">7 </option> </select> <input type=submit> <c:set var="s" value="${param.combo1}" /> Today is <br> <font size=24 color=red> <c:choose> <c:when test="${s==1}">Sunday </c:when> <c:when test="${s==2}">Monday</c:when> <c:when test="${s==3}">Tuesday</c:when> <c:when test="${s==4}">Wednesday</c:when> <c:when test="${s==5}">Thursday</c:when> <c:otherwise> select between 1 & 5 </c:otherwise> </c:choose> </body> </html> --------------------------------------------Demo-5 deals with Iteration tag. We are familiar with the 'for-each' construct. JSTL's 'for-each' also has the same functionality. In the following example, we have a String array. named as 'colors'. By using the <c:forEach> tag, we iterate through the array and display the values. -----------------------------------//demo5.jsp <%@ page contentType="text/html" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <% pageContext.setAttribute("colors",
6
<c:out value="${a.count}"
/> </td>
We get the following display, when we execute the program. 0 1 true false red 1 2 false false green 2 3 false false blue 3 4 false false purple 4 5 false true black =============================================== <c:forEach> action tag contain the following attribute list: items : the collection of items like String[] var : a symbolic name for the collection
begin : the starting index of iteration end : the ending index of iteration step : incremental step
3 4 5 6 7
8 =============================================== Demo7 deals with JSTL's 'forTokens' tag.<c:forTokens>, which iterates over a string of tokens separated by a set of delimiters like the stringTokenizer class in Java. -------------------------------------------demo7.jsp <%@ page contentType="text/html" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <c:set var="s" value="SAM,DELHI,MCA,24,90" /> <html> <body> <table border="1"> <tr> <th>Name</th> <th>Place</th> <th>Degree</th> <th>Age</th> <th>Mark</th> </tr> <tr> <c:forTokens items="${s}" delims="," var="token" > <td><c:out value="${token}" /></td> </c:forTokens> </tr> </table> <br> </font> </body> </html> -----------------------------------------Name Place Degree Age Mark
10