Skip to main content

Getting servlet class from a HttpSession

3 replies [Last post]
dziku
Offline
Joined: 2009-07-14

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?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
b_faissal
Offline
Joined: 2008-11-28

Hi,

When you servlet creates the image then set an attribut in this session indicating that it was created by ...

session.setAttribute("createdByaServlet",true);

and then you can just do this :

public void sessionDestroyed(HttpSessionEvent se) {
if(session.getAttribute("createdByaServlet")){
HttpSession s = se.getSession();
ServletContext sc = s.getServletContext();
File f = new File(sc.getRealPath("\\") + s.getId());
f.delete();
}
}

Lincoln Baxter, III

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@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@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>
[att1.html]

dziku
Offline
Joined: 2009-07-14

The image is generated when the form is randered and saved to disk so that I can use a tag in the html.
I need the session to store the String value shown in the image. Process:
- the user calls a form,
- the image is generated saved to disk and it's number value is stored in a newly created session,
- the user get the html with the image,
- he rewrites the code from the picture to a text field,
- he presses submit button,
- the value he provided is compared to the value previously saved in his session,
- session is invalidated and I want the image to be deleted.

How can I compare the value from the user with the value in the image if I don't have a session?
That's the way I've implemented thsi common mechanism. Mayby it's done in a different way.