Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there away create session in java class NOT servlet class ?

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
im new to servlets , is there any way to create http session in ordinary java class
under tomcat ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't create sessions. The container does it on your behalf.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a reference to a HttpServletRequest to make a new session, so it won't work in an ordinary class.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i understand
maybe i rephrase my question , do i have extend HttpServlet when i like to create sessions before im
entering jsp page?

my flow needs to be :

java class that creates session with complex logic --> forward to jsp page that reads the session data and updates some session attributes .
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be going

-> request comes in
-> call complex method and get the results
-> put the results on the session

The complex class should not need to know about 'sessions' and sessions shouldn't know about complex methods.
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David has said clearly. You better dont mix them.

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.

As HttpServlet is predominant and mostly in use, extending HttpServlet will do because HttpServlet inturn extends GenericServlet which inturn implements the javax.servlet.Servlet interface.

HtH.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Obviously copied material without a source cited removed]
[ May 24, 2007: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic