JSTL Core Tags: General Tags
April 03, 2010 20:21:15 Last update: April 04, 2010 03:30:22
The tags
The
The value can be set from the value attribute or the body of the tag (the body is trimmed before it's processed):
Setting
The
where
The
where
Test it
Make these additions to the expanded test application:
<c:out>
- 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"]/>
whereescapeXmldefaults to true anddefaultdefaults to empty string "".
<c:out value="${expr}" escapeXml="false"/>is equivalent to${expr}.
- If a variable is set in multiple scopes, the lower scope wins. In the following example code,
attribute1is set in request, session, and application scopes;attribute2is set in session and application scopes;attribute3is 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
-
- 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
-
- Please note that
<c:out>does not recognize request parameters or cookies automatically. For example, if a parameter namedparam1is given in the HTTP request,<c:out value="${param1}"/>does not print the value of the parameter. You have to use theparamimplicit 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:
- Create a new directory
handlerunderdemoand create a new Java classCoreBasic: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); } }
- Create a new JSP (
corebasic.jsp) underwebapp:<%@ 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><c:out> test</h2> <p> <c:out value="${'${'}attribute1}"/>: <c:out value="${attribute1}"/><br> <c:out value="${'${'}attribute2}"/>: <c:out value="${attribute2}"/><br> <c:out value="${'${'}attribute3}"/>: <c:out value="${attribute3}"/><br> <c:out value="${'${'}sessionScope.attribute1}"/>: <c:out value="${sessionScope.attribute1}"/><br> <c:out value="${'${'}applicationScope.attribute2}"/>: <c:out value="${applicationScope.attribute2}"/><br> <c:out value="${'${'}param1}"/>: <c:out value="${param1}"/><br> <c:out value="${'${'}param.param1}"/>: <c:out value="${param.param1}"/><br> </p> <h2><c:set> test</h2> <c:set var="attribute1" value="Attribute1 (page scope)"/> <c:out value="${'${'}attribute1}"/>: <c:out value="${attribute1}"/><br> <c:out value="${'${'}requestScope.attribute1}"/>: <c:out value="${requestScope.attribute1}"/><br> <h2><c:remove> test</h2> <c:remove var="attribute1" scope="page"/> <c:out value="${'${'}attribute1}"/> after removal from page scope: <c:out value="${attribute1}"/><br> <c:remove var="attribute1"/> <c:out value="${'${'}attribute1}"/> after removal: <c:out value="${attribute1}"/><br> <h2><c:catch> 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>
- Compile and package the WAR with:
mvn package - Deploy the WAR to a servlet container of your choice (for example, Tomcat 6).
- 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.