how to use jstl variable in a jsp page

Hi all,

I am new to JSTL and i want to access the value of jstl in jsp.

when i use the tag like below it is displaying the value ""

<c:out value="${ack}"/>

But as ack is string, i want to convert it into int. and i have written like

<c:set var="ackvalue" value="${ack}"/>

and int ack = Integer.parseInt(ackvalue);

But it is showing error like varialbe can not be resolved: ackvalue

can anybody please help me regarding this?

Waiting for your warm response.

Thanks in advance

[555 byte] By [sridevi123a] at [2007-10-3 4:50:56]
# 1

JSTL doesnt work with scripting variables (variables in scriptlets).

JSTL sets/gets variables in scope(page,request,session, application).

Thus

<%

int ack = Integer.parseInt(ackvalue);

pageContext.setAttribute("ackValue", new Integer(ack)); //set the Ineteger object in page scope

%>

<c:out value = "${ackValue}"/>

Seeing that you use the integer conversion for display purposes (ie in a c:out tag), it converts the Integer back to String. So you may as well have

<%pageContext.setAttribute("ackValue", ack);%>

<c:out value = "${ackValue}"/>

Does that help?

ram.

Madathil_Prasada at 2007-7-14 22:55:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Instead you can use the <bean:define ../> tag in struts as below ..

<bean:define id="ackvalue" value="333"/>

<%

int ack = Integer.parseInt(ackvalue);

%>

The value is : <%=ack%>

Cheers

-Rohit

RohitKumara at 2007-7-14 22:55:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi,

Thanks for your immediate reply.

I am able to display the ackvalue when i am using

<c:set var="ackvalue" value="${ack}"/>

and <c:out value="${ackvalue}"/>

But my actual requirement is to use the ack value in switch statement. if i can assign the value to a String variable, then it si easy for me to proceed. Please help me in this regard.

Thanks,

sridevi123a at 2007-7-14 22:55:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
<c:set var="ackvalue" value="${ack}" scope="request"></c:set> Object myAckvalue = request.getAttribute("ackvalue");
golanblna at 2007-7-14 22:55:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> Hi,

> Thanks for your immediate reply.

> I am able to display the ackvalue when i am using

> <c:set var="ackvalue" value="${ack}"/>

> and <c:out value="${ackvalue}"/>

> But my actual requirement is to use the ack value in

> switch statement. if i can assign the value to a

> String variable, then it si easy for me to proceed.

> Please help me in this regard.

>

> Thanks,

You can use the c:choose, c:when and c:otherwise tags

<c:choose>

<c:when test = "${ackvalue == 1}">

//do stuff

</c:when>

<c:when test = "${ackvalue == 2}">

//do some other stuff

</c:when>

<c:otherwise>

//stuff

</c:otherwise>

</c:choose>

ram.

Madathil_Prasada at 2007-7-14 22:55:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Hi,Thank you very much.. This has worked out<c:set var="ackvalue" value="${ack}" scope="request"></c:set> Object myAckvalue = request.getAttribute("ackvalue");Thank you
sridevi123a at 2007-7-14 22:55:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
I will add my vote to rams here and say you should not be doing scriptlet code on a JSP.The whole point of JSTL is to eliminate scriptlet code from your pages. The JSTL choose/when syntax is a perfect substitute for a switch statement in scriptlet code.
evnafetsa at 2007-7-14 22:55:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...