webtier@glassfish.java.net

Re: [webtier] Getting servlet class from a HttpSession

From: Lincoln Baxter, III <lincolnbaxter_at_gmail.com>
Date: Mon, 20 Jul 2009 18:33:40 -0400

I suppose my question is: Why are you doing this effectively at the
scope of a Session, when I believe what you need is a per-Request image
generation and code storage mechanism? Generate the image for a each new
time the form is rendered -- then you would not need to save the image
between requests or sessions.

Is this along the lines you were thinking of?

--Lincoln

On Mon, 2009-07-20 at 15:25 -0700, webtier_at_javadesktop.org wrote:

> I'm creating a registration mechanism that requires providing a number pictured in an image generated on the fly. The registration servlet creates a session, an image and a form to fill. The filename of the image is equal to the session id. The image is embeded in the servlet-generated html and displayed. The class used to generate the image also returns the number that the image shows. I'm storing its value in the session after the image is created. As soon as the user submits the form the number value they've provided is compared to the one stored in the session. It works quite well. There is a small problem related to the form page being cached (when you reopen the form it needs to be refreshed otherwise it shows an old image while the generated number value is new) but I think it's fixable. The bigger problem is deleting the image when it's no longer needed.
>
> I've created a class implementing the HttpSessionListener interface in order to delete the image file when the session is destroyed. The code that is supposed to do that looks like this:
>
> public void sessionDestroyed(HttpSessionEvent se) {
> HttpSession s = se.getSession();
> ServletContext sc = s.getServletContext();
> File f = new File(sc.getRealPath("\\") + s.getId());
> f.delete();
> }
>
> The only problem is that each session will run this code while invalidated. I would like to make this code run only for sessions that are created in the registration servlet. However, I don't know how to determine the servlet where the session originated. It's impossible to check or set any session attribute after it's been invalidated. There's no way to get a servlet name from the ServletContext, either (methods deprecated).
>
> Anybody has any ideas how to improve the code above?
> [Message sent by forum member 'dziku' (dziku)]
>
> http://forums.java.net/jive/thread.jspa?messageID=356654
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: webtier-help_at_glassfish.dev.java.net
>