my dog learned polymorphism*
The moose likes Servlets and the fly likes Is there away create session in java class NOT servlet class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is there away create session in java class NOT servlet class ?" Watch "Is there away create session in java class NOT servlet class ?" New topic
Author

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

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
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

Joined: Jan 10, 2002
Posts: 60747
    
  65

You don't create sessions. The container does it on your behalf.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You need a reference to a HttpServletRequest to make a new session, so it won't work in an ordinary class.


[My Blog]
All roads lead to JavaRanch
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: May 14, 2007
Posts: 244
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.


SCJP<br />SCWCD 1.4 Upgrade (Remember: me stupid)<br />SCWCD 1.4<br /><a href="http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html" target="_blank" rel="nofollow">SCBCD 5.0</a><br /><a href="http://www.enthuware.com" target="_blank" rel="nofollow">SCBCD 5.0 mock exam</a> <br /> <br />SCEA 5 Part1: Preparing...
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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
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
Marshal

Joined: Mar 22, 2005
Posts: 41048
    
  43
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.


Ping & DNS - my free Android networking tools app
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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: 41048
    
  43
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

Joined: May 24, 2007
Posts: 4
[Obviously copied material without a source cited removed]
[ May 24, 2007: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60747
    
  65

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.
 
jQuery in Action, 2nd edition
 
subject: Is there away create session in java class NOT servlet class ?
 
Similar Threads
Statelesss Session Bean Vs Java class
Hibernate example
Bean will not remember values
To Call Session bean from a java class loaded into Oracle Database.
singleton stateless session bean