![]()  | 
  | 
,
 
0 Comments
)
  | 
| Subject:
Access Java variable in JSTL tag
 Category: Computers > Programming Asked by: jonra-ga List Price: $5.00  | 
Posted:
08 Jul 2004 04:13 PDT
 Expires: 07 Aug 2004 04:13 PDT Question ID: 371241  | 
JSP/JSTL: 
I have a JSP page with some sciptlets
<%
String title="My page"
%>
I want to use <c:out value='${title}' /> JSTL tag to output the result.
Can I access the variable in the tag? | 
  | 
| Subject:
Re: Access Java variable in JSTL tag
 Answered By: rhansenne-ga on 08 Jul 2004 09:02 PDT Rated: ![]()  | 
Hi jonra-ga , If you want to use a scriptlet to declare the variable and the c:out tag to print it, a valid JSP page might look like this: <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html> <head> <title>sample jsp page</title> </head> <body> <% // declare or construct your variable... String title="My page"; // set the variable in the page context under the variable name "title" pageContext.setAttribute("title",title); %> <c:out value='${title}' /> </body> </html> You can also declare variables using only jstl, in stead of through a scriptlet: <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html> <head> <title>sample jsp page</title> </head> <body> <c:set var="title" scope="page" value="My page"/> <c:out value='${title}' /> </body> </html> Of course if you're using scriptlets anyway, you could also use the following scriptlet to print the variable: <html> <head> <title>sample jsp page</title> </head> <body> <% String title="My page"; %> <%=title%> </body> </html> The jstl libraries should also be present in the classpath of your web application. Here are some useful JSTL primers and references: IBM - A JSTL primer: http://www-106.ibm.com/developerworks/java/library/j-jstl0211.html SUN - JavaServer Pages Standard Tag Library http://java.sun.com/webservices/docs/1.0/tutorial/doc/JSTL.html JSP Syntax reference sheet: http://www.oio.de/public/java/jstl-reference/jstl-reference-2004.pdf Best regards, rhansenne-ga.  | |
  | |
  | |
jonra-ga
rated this answer: 
 | 
  | 
| There are no comments at this time. | 
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.  | 
| Search Google Answers for | 
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |