Java scriptlets & JSTL intercommunication

I’m trying to figure out a way to make variables declared within Java scriptlets (in a jsp page) visible to my JSTL expressions.
i.e. :

<%
double myDouble = request.getparameter(“number”);
//some code doing stuff to the above variable…
%>

…further down…

<c:out value=“${myDouble}”/>

except the <c:out> tag does not know what myDouble is, and blows up. Is there a way to give the primitive the proper scope, or whatever, to allow it to be seen by the <c:out>? And, yes, I know there are other ways to accomplish this, but my “example” above is an over-simplified look at the type of problem I’m dealing with.

Thanks.

${myDouble} looks like velocity. Which if you’re using velocity, then I believe you can’t use JSTL.

BUT, if you’re not using velocity then try using <%= myDouble %> instead.

Regards,

Nate

Unfortunately, I can’t use <%=variableName%>. I’m trying to reference these variables from within <c:foreach> tags, and <sql:query> tags, to name a few. Does anyone have a lifesaving trick that will get me out of this jam?

Thanks.

Try this:


<%
double myDouble = request.getparameter("number");
//some code doing stuff to the above variable...
pageContext.setAttribute("myDouble", myDouble);
%>

It also works the other way. If you want a scriptlet to access a variable declared using jstl do:


<%
String someVar = (String) pageContext.getAttribute("someVar");
%>

Hope this helps.

mark

What Velocity ? That’s the JSP Expression Language.

You cannot do that directly because the whole point of the Expression Language is to prevent as much as possible the use of servlets. You could however do somthing like:


<%! double myDouble; %>
<%
    myDouble = request.getparameter("number");
    pageContext.setAttribute("myDouble", myDouble);
%>

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

Anyway, try to avoid that. If you need local objects use javabeans and avoid mixing servlet code with JSP tags and EL.
Here is a good article about the Expression Language:

How come you aren’t using <c:set> to make the variables?
This should also work:

<c:set var="myDouble" value="${number}"/>