JSTL Core Tags: General Tags 

Joined:
08/13/2009
Posts:
164

April 03, 2010 20:21:15    Last update: April 04, 2010 03:30:22
The tags
<c:out>
  1. The <c:out> tag evaluates an expression and outputs the result on the page. The syntax is:

    <c:out value="value" [escapeXml="{true|false}"] [default="defaultValue"]/>
    


    where escapeXml defaults to true and default defaults to empty string "".

    <c:out value="${expr}" escapeXml="false"/> is equivalent to ${expr}.

  2. If a variable is set in multiple scopes, the lower scope wins. In the following example code, attribute1 is set in request, session, and application scopes; attribute2 is set in session and application scopes; attribute3 is set in the application scope. The results are:
    • <c:out value="${attribute1}"/>: Attribute1 request scope
    • <c:out value="${attribute2}"/>: Attribute2 session scope
    • <c:out value="${attribute3}"/>: Attribute3 application scope


  3. To access values in higher scopes, you have to specify the scope explicitly, like this:
    • <c:out value="${sessionScope.attribute1}"/>: Attribute1 session scope
    • <c:out value="${applicationScope.attribute2}"/>: Attribute2 application scope


  4. Please note that <c:out> does not recognize request parameters or cookies automatically. For example, if a parameter named param1 is given in the HTTP request, <c:out value="${param1}"/> does not print the value of the parameter. You have to use the param implicit variable:
    <c:out value="${param.param1}"/>
    



<c:set>

The <c:set> tag sets the value of a scoped variable (page, request, session or application scope, the default is page). It can also set a property of a target object.

The value can be set from the value attribute or the body of the tag (the body is trimmed before it's processed):

<c:set var="varName" value="theValue" scope="request"/>

<c:set var="varName" scope="session">
    The body is the value of the variable. EL can be used, such as ${attribute1}.
</c:set>

<c:set target="targetObject" property="propertyName" value="${expr}"/>

<c:set target="targetObject" property="propertyName">
The body is the value to be set.
</c:set>


Setting attribute1 in page scope:
<c:set var="attribute1" value="Attribute1 (page scope)"/>


<c:remove>

The <c:remove> tag removes a scoped variable. The syntax is:
<c:remove var="variableName" [scope="{page|request|session|application}"]/>

where scope is optional. The variable is removed from all scopes if scope is not given.

<c:catch>

The <c:catch> tag catches a java.lang.Throwable thrown by its nested actions. The syntax is:
<c:catch [var="varName"]>
        nested actions
</c:catch>

where var is optional. If given, var is created in page scope and stores the exception caught.

Test it
Make these additions to the expanded test application:
  1. Create a new directory handler under demo and create a new Java class CoreBasic:
    package jstl.demo.handler;
    
    import java.io.IOException;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    import jstl.demo.DemoHandler;
    
    public class CoreBasic implements DemoHandler {
        public void handleRequest(HttpServletRequest req,
    			      HttpServletResponse resp)
    		throws IOException, ServletException {
        	HttpSession sess = req.getSession();
    	ServletContext app = sess.getServletContext();
    
    	// set attribute1 in request, session and application level
    	req.setAttribute("attribute1", "Attribute1 (request scope)");
    	sess.setAttribute("attribute1", "Attribute1 (session scope)");
    	app.setAttribute("attribute1", "Attribute1 (application scope)");
    
    	// set attribute2 in session and application scope
    	sess.setAttribute("attribute2", "Attribute2 (session scope)");
    	app.setAttribute("attribute2", "Attribute2 (application scope)");
    
    	// set attribute3 in application scope
    	app.setAttribute("attribute3", "Attribute3 (application scope)");
    
    	RequestDispatcher d = req.getRequestDispatcher("/corebasic.jsp");
    	d.forward(req, resp);
        }
    }
    

  2. Create a new JSP (corebasic.jsp) under webapp:
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <html>
    <head>
    <title>JSTL Basic Core Tags</title>
    
    <style type="text/css">
    body {
        font-family: sans-serif;
    }
    </style>
    </head>
    
    <body>
    <h2>&lt;c:out&gt; test</h2>
    <p>
    &lt;c:out value="${'${'}attribute1}"/&gt;: <c:out value="${attribute1}"/><br>
    &lt;c:out value="${'${'}attribute2}"/&gt;: <c:out value="${attribute2}"/><br>
    &lt;c:out value="${'${'}attribute3}"/&gt;: <c:out value="${attribute3}"/><br>
    &lt;c:out value="${'${'}sessionScope.attribute1}"/&gt;: 
    <c:out value="${sessionScope.attribute1}"/><br>
    &lt;c:out value="${'${'}applicationScope.attribute2}"/&gt;: 
    <c:out value="${applicationScope.attribute2}"/><br>
    &lt;c:out value="${'${'}param1}"/&gt;: <c:out value="${param1}"/><br>
    &lt;c:out value="${'${'}param.param1}"/&gt;: <c:out value="${param.param1}"/><br>
    </p>
    
    <h2>&lt;c:set&gt; test</h2>
    <c:set var="attribute1" value="Attribute1 (page scope)"/>
    &lt;c:out value="${'${'}attribute1}"/&gt;: <c:out value="${attribute1}"/><br>
    &lt;c:out value="${'${'}requestScope.attribute1}"/&gt;: 
    <c:out value="${requestScope.attribute1}"/><br>
    
    <h2>&lt;c:remove&gt; test</h2>
    <c:remove var="attribute1" scope="page"/>
    &lt;c:out value="${'${'}attribute1}"/&gt; after removal from page scope:
    <c:out value="${attribute1}"/><br>
    
    <c:remove var="attribute1"/>
    &lt;c:out value="${'${'}attribute1}"/&gt; after removal:
    <c:out value="${attribute1}"/><br>
    
    <h2>&lt;c:catch&gt; test</h2>
    <c:catch var="ex">
        Content before exception...
        <% int x = 1/0; %>
        content after exception.
    </c:catch>
    
    <p>
    <c:if test="${not empty ex}">
        Exception: <c:out value="${ex}"/>
    </c:if>
    </p>
    
    </body>
    </html>
    

  3. Compile and package the WAR with: mvn package
  4. Deploy the WAR to a servlet container of your choice (for example, Tomcat 6).
  5. Test the page with this URL (Tomcat 6 running on port 8080):
    http://localhost:8080/jstl-demo/demo/CoreBasic?param1=value1

    You may adjust the URL if your servlet container runs on a different port or the web app is bound to a different context root.
Share |
| Comment  | Tags