Hi, I'm not sure about your problem. However, can't you make the complex work into a servlet that, as a final action, dispatches the request to your jsp? Alternatively, you might create a ThreadLocal containing your session that is initialized and released with a filter. But, saying the truth, I don't seem a good idea.
In case of jsp, you will get access to 'session' as its one of the implicit objects and will be accessible without declaring/importing it.
In case of Servlet, you will have to have a reference to HttpServletRequest with which you can request for a session through getSession(boolean) method. As marc said, the container is gonna give you.
If at all a java class has to be a Servlet, it needs to be a part/member of Servlet Arena. Any class becomes a member by either extending any of the GenericServlet/HttpServlet or implementing the javax.servlet.Servlet interface. Without which the class can't be a Servlet.
Originally posted by Satou kurinosuke: You need a reference to a HttpServletRequest to make a new session, so it won't work in an ordinary class.
Satou, thats would be tricky for a newbie.
So, the bottomline is you don't make sessions - container does that for you, as already said by Bear.
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
posted
0
ok thanks for the fast reply folks , what if i like to know how to do it with ordenry class? where can i find info or tutorial ? another question , can i tell tomcat to run ordenry java class first when my webapp is started , i know how to do it with servlet , but not with simple class thanks
what if i like to know how to do it with ordenry class?
You need a reference to an HttpServletRequest from which you can get a session. Whether the code to get the session is in the servlet class or some other class is immaterial, but as pointed out, for the sake of separating concerns, you should do it in the servlet.
can i tell tomcat to run ordenry java class first when my webapp is started , i know how to do it with servlet , but not with simple class
If you're using at least Servlet API 2.3, then you can use the javax.servlet.ServletContextListener to run code at startup time. That's much to be preferred over the load-at-startup setting in web.xml.
If you're using at least Servlet API 2.3, then you can use the javax.servlet.ServletContextListener to run code at startup time. That's much to be preferred over the load-at-startup setting in web.xml.
And just to make sure there's no confusion, don't expect to make a new session from a ServletContextListener.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35429
9
posted
0
And just to make sure there's no confusion, don't expect to make a new session from a ServletContextListener.
Good point. A session is associated with a particular web client -which is represented by a request-, so a session without a request makes no sense.
liaa tras, please do not copy information from other sites and post them here. Either provide a link, or enter the information in your own words. And please don't enter information that really has little to do with the issue under discussion.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Is there away create session in java class NOT servlet class ?