JSTL Help

Hello All,

I need some help with JSTL tags. One with retreiving values from request object and other with storing the values in session.

I trying to replace scriptlet code with JSTL tags.

Here is my scriplet code.

[b]<%

session.setMaxInactiveInterval(-1);

if(request.getParameter("username").equalsIgnoreCase("CUSTOMER")){

session.setAttribute("hello","SomeValue");

}

else

if (request.getParameter("username") ==null){

if(session.getAttribute("hello") ==null){

session.setAttribute("hello","Someothervalue");

}

}

[/b]

I am trying to access the hello variable from other page.

by using

<%=request.getSession(false).getAttribute("hello")%>

Could anyone provide with some direction.

I am using core tags to do the if else logic.

here is my JSTL Code.

<c:choose>

<c:when test="${${param.username} eg 'Customer' }">

<c:set var="hello?scope="session" value=?KnownUser?/>

</c:when>

<c:otherwise>

<c:set var="hello?scope="session" value=擠efaultUser?/>

</c:otherwise>

</c:choose>

And trying to access the variable value in another page by

<c:out value=?{hello}?scope="session" />

I am getting error message saying that session has no value.

Please let me know what is wrong with the above code.

[2047 byte] By [kris10a] at [2007-11-26 18:11:14]
# 1

Hello All,

I got everything working. However I have one question.

Is there anyway that I can set to see that my sesssion values don't expire in JSTL.

I was doing it using

session.setMaxInactiveInterval(-1);

How do I the same using JSTL ?

Let me know.

Thanks

kris10a at 2007-7-9 5:43:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I quickly skimmed through the JSTL specification, it doesn't mention anything about the equivalent of session.setMaxInactiveInterval(-1);

I think you can set the session inactive internal in your webserver's configuration files.

For example in Tomcat it can be set inside Tomcat's web.xml

located under

%CATALINA_HOME%/conf/web.xml

<session-config>

<session-timeout>30</session-timeout>

</session-config>

Also if you're using sessions inside each JSP page, then its a good idea to prevent the browsers from caching the page because, if the browser caches the page the session won't be active anymore.

Here's some code to prevent caching by browsers (The code is browser dependent)

<%

Calendar c = Calendar.getInstance();

/*c.set(2006, 10, 15);*/

long date = c.getTime().getTime();

if (response.containsHeader(Constant.LAST_MODIFIED) == false) {

response.addDateHeader(Constant.LAST_MODIFIED, date);

} else {

response.setDateHeader(Constant.LAST_MODIFIED, date);

}

%>

<%

response.setHeader( "Expires", "Sat, 6 May 1995 12:00:00 GMT" );

/* set standard HTTP/1.1 no-cache headers*/

response.setHeader( "Cache-Control", "no-store, no-cache, must-revalidate" );

/* set IE extended HTTP/1.1 no-cache headers*/

response.addHeader( "Cache-Control", "post-check=0, pre-check=0" );

/* set standard HTTP/1.0 no-cache header*/

response.setHeader( "Pragma", "no-cache" );

%>

If the above code is inside an include file it won't be effective, it must be inside the JSP that uses the session.

I guess there are better ways to prevent caching of a JSP page by browser, but that's all I know for now.

appy77a at 2007-7-9 5:43:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Thanks for your response. I am not supposed to use scriplets in my code. That's the reason I replaced scriplet code with JSTL. I will try setting the timeout value. Any other suggestions let me know.Thanks
kris10a at 2007-7-9 5:43:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
You're welcome. I wish I knew how to switch-off caching of the page without using scriptlets.Perhaps there's a setting in the webserver similar to session time out setting that allows one to switch-off browser/ proxy caching site-wide, but I don't know about that at this time.
appy77a at 2007-7-9 5:43:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Rather than setting the headers in scriptlet, you can use meta tags in your jsp:

<meta HTTP-EQUIV="pragma" CONTENT="no-cache">

<meta HTTP-EQUIV="cache-control" CONTENT="no-cache">

<meta HTTP-EQUIV="Expires" CONTENT="-1">

evnafetsa at 2007-7-9 5:43:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Thanks for your reply.

I wonder if the HTML meta tags have the same effect as the Java setHeader tags. I'll have to experiment and verify it.

I discovered one more place where one could disable caching. It's a setting in the web applications context file located under:

<TOMCAT_FOLDER>\conf\Catalina\localhost\webapplication_name.xml

One can set cachingAllowed="false" , on the Context node but the definition for this attribute says "If the value of this flag is true, the cache for static resources will be used. If not specified, the default value of the flag is true." over here : http://tomcat.apache.org/tomcat-5.0-doc/config/context.html

so I don't think it applies to JSPs, probably only applies to HTML and images.

In any case whether header or meta caching is disable, I found it difficult to prevent browsers from caching stuff, so sometimes my sessions don't work when the user presses the back button. But I haven't throughly tested it.

appy77a at 2007-7-9 5:43:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...