views:

108

answers:

3

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

+1  A: 

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
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
Oh! okay. what are you up to then? I mean what kinda application you are trying to develop.
Adeel Ansari
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
@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
@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
Why not just `HttpSession session = request.getSession();` ?
BalusC
@BalusC: Oh yeah. Not in touch with these things for quite a time. Thanks, edited.
Adeel Ansari
A: 

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
@Sam, 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
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
session is automatically set up as a variable in a JSP page, just like request is.
Sam Day
@Sam, 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
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
A: 

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 :) ).

BalusC