• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't create sessions. The container does it on your behalf.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Obviously copied material without a source cited removed]
[ May 24, 2007: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic