Google Answers Logo
View Question
 
Q: Access Java variable in JSTL tag ( Answered 4 out of 5 stars,   0 Comments )
Question  
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?
Answer  
Subject: Re: Access Java variable in JSTL tag
Answered By: rhansenne-ga on 08 Jul 2004 09:02 PDT
Rated:4 out of 5 stars
 
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.

Request for Answer Clarification by jonra-ga on 08 Jul 2004 09:21 PDT
Hi.

Thanks for your information.
I have an existing application with a lot of existing <%= data %>
outputs to the JSP. I have changed app server, and any value that is
null, prints "null" to the page. That's why I need to refractor my
code.

<c:out /> clean up the output and replaces null with "".
There are quite a few pages in the app, so I need to make the change
as smooth as possible.

That means that I cannot re-declare all the variables, as suggested.

Is there another way of passing in my existing strings into the tags?

Thanks,

Jon

Clarification of Answer by rhansenne-ga on 09 Jul 2004 15:29 PDT
Hi jonra-ga ,

Unfortunately, the c:out tag can only access variables which have been
placed in the page, request, session or application context.

You could however refactor your pages by replacing:

<%= data %>

by

<%= (data==null?"":data) %>

This would also ensure that null's are replaced by empty strings. (If
you know a little about regular expressions, this could even be done
by an automatic find and replace).


Sincerely,

rhansenne-ga.
jonra-ga rated this answer:4 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

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 Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy