5

I need to write a small Java class that will enable me to add to and read from the current user session.

Everything I see refers to Servlets but I'd ideally like to just use a plain old class.

Can anyone please help this Java Newbie?

Thanks

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
griegs
  • 22,624
  • 33
  • 128
  • 205

4 Answers4

2

The general concept of a "Session" is really just a data storage for an interaction between a HTTP client and server. Session management is automatically handled by all HTTP Servlets. What framework?

If you're just wanting to store information for a console app to remember information between runs, then consider using Xml to save/load data from a file.

Sam Day
  • 1,713
  • 9
  • 17
  • 1
    I know what a session is. What I want is the ability to call a class from my jsp page and read from and write to the session. – griegs Sep 20 '10 at 03:33
  • Oh if you want to access session from JSP that's as simple as something like this: session.setAttribute("test", "value");. Take a look at http://www.jsptut.com/Sessions.jsp – Sam Day Sep 20 '10 at 03:37
  • 1
    session is automatically set up as a variable in a JSP page, just like request is. – Sam Day Sep 20 '10 at 03:40
  • 1
    I know how to access the session in a jsp page, what I'm after is accessing the session from a class file that is referenced in the jsp – griegs Sep 20 '10 at 03:47
  • NB: A JSP compiles to a servlet when in use. So for a class to access the session, it need to get access exactly the same way as if the class we being referenced from a serlet. – Jaydee Sep 20 '10 at 08:43
1

Yes, just pass the HttpRequest to your class from your servlet.

In your servlet do something like this,

cmd.execute(request);

In your class do something like this,

public class Command implements ICommand {
   .
   .
   public void execute(HttpServletRequest request){
      HttpSession sess = request.getSession(false);
   }
   .
   .
   .
}
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
  • The reason I don't want to use a servlet is because I don't know how to use them hence wanting just a class. If you can provide sample code tho... – griegs Sep 20 '10 at 03:31
  • 2
    Oh! okay. what are you up to then? I mean what kinda application you are trying to develop. – Adeel Ansari Sep 20 '10 at 03:36
  • Trying to develop a web application and i want a centralised place to manage my session. I thought I coupld simply import a library to gain access to the session but i now think i need to pass the httpsession to my class – griegs Sep 20 '10 at 03:39
  • 1
    @griegs: You are right in assuming that. You must use Servlet to develop what we call a web app in Java. By using some high level framework you might get a feel that you manage to do it without Servlets, but that would be just a bogus impression. Knowing servlet is a must. – Adeel Ansari Sep 20 '10 at 03:42
  • @griegs: And its definitely better to pass a request instead. Because you are gonna need few more things in near future, not just HttpSession. – Adeel Ansari Sep 20 '10 at 03:43
  • 2
    Why not just `HttpSession session = request.getSession();` ? – BalusC Sep 20 '10 at 11:29
  • @BalusC: Oh yeah. Not in touch with these things for quite a time. Thanks, edited. – Adeel Ansari Sep 21 '10 at 03:12
1

Use a component based MVC framework which abstracts all the ugly Servlet API details away so that you ends up with zero javax.servlet imports. Examples of such are JSF2 and Struts2.

In JSF2 for example, you'd just declare User class as a session scoped managed bean:

@ManagedBean
@SessionScoped
public class User {
    // ...
}

Then in the "action" bean which you're using to processing the form submit, reference it as managed property:

@ManagedBean
@RequestScoped
public class SomeFormBean {

    @ManagedProperty(value="#{user}")
    private User user;

    public void submit() {
        SomeData someData = user.getSomeData();
        // ...
    }
}

That's it.

If you'd like to stick to raw Servlet API, then you've to live with the fact that you have to pass the raw HttpServletRequest/HttpServletResponse objects around. Best what you can do is to homegrow some abstraction around it (but then you end up like what JSF2/Struts2 are already doing, so why would you homegrow -unless for hobby/self-learning purposes :) ).

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

In general, as mentioned in the other answers, session in many ways acts as a store. So to interact wth a session from another class which is outside of the Servlet/JSP framework the reference to the session in question needs to be procured. There are few ways it can be achieved:

1) Passing the session as part of a method parameter (already mentioned in other answers) 2) Binding the session to a thread local variable on to the current thread executing (refer ThreadLocal). This method has the advantage of not declaring specific parameters on the method signature of the class that needs to use the session. In addition, if the calling thread goes through a library and then again calls some specific class e.g. Servlet->YourClass0 -> Apache Some Library -> YourClass1, the session will also be available to YourClass1.

However, the thread local also needs to be cleared when the executing thread returns through the initial component (servlet let's say) otherwise there certainly could be memory leaks.

In addition, please refer to your specific framework for treatement of sessions, the above mechanism works fine in Tomcat.

Ironluca
  • 3,402
  • 4
  • 25
  • 32