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]

# 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.
# 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.