You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@jakarta.apache.org by Glenn Nielsen <gl...@voyager.apg.more.net> on 2000/12/10 16:16:01 UTC

[Fwd: Re: jsp:useBean with session scope]

One minor enhancement to the JSP pages below, I encoded the URL
for cookies.jsp so that the original session created could be
removed if the user doesn't have cookies enabled.

In this case I would do the following.
My example uses some JSP 1.1 tags from Jakarta-taglibs.

Include the below in each JSP page to test for cookies:

<%-- initialize jsp tag libraries --%>
<%@ taglib uri="http://jakarta.apache.org/taglibs/response-1.0" prefix="res" %>
<%@ taglib uri="http://jakarta.apache.org/taglibs/request-1.0" prefix="req" %>
<%@ taglib uri="http://jakarta.apache.org/taglibs/session-1.0" prefix="sess" %>

<%-- Get a script variable for this HTTP Request --%>
<req:request id="req"/>

<%-- Make sure user has cookies enabled --%>
<req:issessionfromcookie value="false">
 <%-- Users HTTP request did not contain a cookie, user has no previous session --%>
 <%-- Setup redirect back to the page requested --%>
 <sess:setattribute name="redirect"><jsp:getProperty name="req"
property="requestURL"/>?<jsp:getPrope
rty name="req" property="queryString"/></sess:setattribute>
 <%-- Setup a test cookie to return to user --%>
 <res:addcookie name="CookiesEnabled" value="true"></res:addcookie>
 <%-- Now send back a redirect to check if user has cookies enabled. --%>
 <%-- encode the URL with the sessionID just in case user doesn't
      have cookies enabled. This makes sure session can be removed by cookies.jsp.  --%>

<res:sendredirect><res:encoderedirecturl>cookies.jsp</res:encoderedirecturl></res:sendredirect>
</req:issessionfromcookie>


And here is cookies.jsp

<%-- Cookies JSP, determines if user has cookies enabled --%>

<%-- initialize jsp tag libraries --%>
<%@ taglib uri="http://jakarta.apache.org/taglibs/request-1.0" prefix="req" %>
<%@ taglib uri="http://jakarta.apache.org/taglibs/response-1.0" prefix="res" %>
<%@ taglib uri="http://jakarta.apache.org/taglibs/session-1.0" prefix="sess" %>

<req:issessionfromcookie value="false">
 <%-- User does not have cookies enabled, return back a
      page telling them they must enable cookies --%>
 <html>
 <head>
  <title>You must enable cookies</title>
 </head>

 <body bgcolor="#ffffff" text="#000000" link="#0033cc" vlink="#663366" alink="#ff3300">

 <%-- Display forum banner --%>
 <center>
  <h1>You must enable cookies</h1>
 </center>

 <hr size="0">
 <CENTER>
  You need to enable Cookies in your web browser.
 </CENTER>
 </body>
 </html>
 <%-- Invalidate the users session so it can be recycled
      by the servlet container.  --%>
 <sess:invalidate/>
 <%-- Thats all, send the above back to user telling
      them they must enable cookies --%>
 <res:skippage/>
</req:issessionfromcookie>

<%-- Redirect users back to the page they were on that
     triggered the cookie check --%>
<sess:existsattribute name="redirect">
 <res:sendredirect><sess:attribute name="redirect"/></res:sendredirect>
</sess:existsattribute>
<%-- If there is no redirect setup, send them back to main page --%>
<res:sendredirect>index.jsp</res:sendredirect>

Horace Vallas wrote:
> 
> Hi - I've been playing around with TC3.2 and have a question
> about what happens when a person doesn't accept the session cookie...
> 
> say ...
> 1. there's a page which includes a bean with session scope
> 2. a new visitor hits that page first
> 3. the person doesn't accept the cookie
> 4. the person loads another page which includes the same bean also
>    with session scope and, again, doesn't accept the cookie
> 5. etc.
> 
> the session scoping works fine when the cookie is accepted - BUT -
> when the cookie is not accepted, then it looks like the resources
> (mainly memory) consumed by the bean in that page (2 & 4 above for
> example) remain locked somehow.
> 
> I noticed it accidentally when testing a query handling bean that
> keeps a ResultSet around from page to page within a session (to keep
> from having to re-perform the query on successive previous|next pages).
> I was playing with a case that returned a large ResultSet and found
> that, if I didn't accept the cookie on successive previous|next pages,
> I would run out of memory after a few steps in the sequence. I could
> watch the memory remaining used with top as I moved from page to page.
> 
> Is there something that I must do to free up the memory consumed by
> a bean with session scope if the person doesn't actually accept the
> session cookie?
> 
> --
> Wishing you an "OOBA OOBA" Y2K
> Horace                            ...once known as "Kicker" :-)
> ================================================================
> Horace Vallas   hav.Software                 http://www.hav.com/
>                 P.O. Box 354                         hav@hav.com
>                 Richmond, Tx. 77406-0354     voice: 281-341-5035
>                 USA                            fax: 281-341-5087
> 
> Thawte Web Of Trust Notary in SW Houston, Tx.
> http://www.hav.com/?content=/thawteWOTnotary.htm
> ================================================================
> ...drop by and chat if I'm online       http://www.hav.com/chat/
> ===   ===   ===   ===   ===   ===   ===   ===   ===   ===   ====
> What is a Vet? ... He is the barroom loudmouth, dumber than five
> wooden planks, whose overgrown frat-boy behavior is outweighed a
> hundred times in the cosmic scales by four hours of exquisite
> bravery near the 38th parallel. ... - Unknown
>                                       http://www.hav.com/vet.htm
> ================================================================

-- 
----------------------------------------------------------------------
Glenn Nielsen             glenn@more.net | /* Spelin donut madder    |
MOREnet System Programming               |  * if iz ina coment.      |
Missouri Research and Education Network  |  */                       |
----------------------------------------------------------------------