• 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

session without request

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think my question will not be possible but i am standing in that scenario.
Container is tomcat 6. Is there any way to get the session object without the help of request in the servlet or any model class from tomcat? Reason is, there is a class which was used in more then 500 other class(utility class). currently i need the session object in that class but i cant able to change all the 500 class to pass request argument. Is there any way to achieve it?
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try with putting the object into the ServletContext...!!!

That may be the option..but that also depend upon your design of application.

 
Sheriff
Posts: 67746
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
Obviously not. Without a request, how would the container know which session to hook up to.

Sessions should rarely/never be needed in a utility class -- as with any other container-managed object. What is it from the session that the utility class needs?
 
Arvind Subramanian
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bibeault,
I will tell more detailed.
While making dbconnection i will have connection string in session and create new connection for every request till now. But now i want to create only one connection per session. So to the class we create connection we will give simple string(connection string) till now, but now i need to provide the session itself which has databaseutils(object which holds connection) object.

Recently me n my frnd Praveen formed the plan to keep the databaseutils in hashmap(key: sessionid, value:databaseutils) and place it in context (as told by Porlekar), and we are goin to change the connection string which is in session to session-id, so from that we can get the corresponding databaseutils for that session by getting it from that context attribute.


We have the plan , have to trail it. If succeed i will post it.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i think you can but you will have to set session in your model using jsp or servlet class suppose that your class is test in which you need session and the servlet is Check then you can do that in this way.


public class Check extends HttpServlet{

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session= req.getSession();
Test test = new Test();
test.setSession(session);
}

class Test
{
HttpSession sessionObject = null;
public void setSession(HttpSession session)
{
this.sessionObject = session;
}
}
}


now your session object is same as the servlet session
 
Bear Bibeault
Sheriff
Posts: 67746
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

Arvind Subramanian wrote:But now i want to create only one connection per session.


Bad, bad idea.

The only proper way to deal with connections is to use a robust connection pool, preferably container-managed.
 
Arvind Subramanian
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Bear Bibeault wrote:

Arvind Subramanian wrote:But now i want to create only one connection per session.


Bad, bad idea.
The only proper way to deal with connections is to use a robust connection pool, preferably container-managed.



Even now connection are managed by pool only, and also additionally hold by a custom made class. I also accept that maintain connection in session level is foolish but no other way. As i need to rollback all the transaction made on that session when it dies. Is any other good framework available for it?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK! Holding on to a reference to ANY object that is managed by the container is a bad bad bad bad bad idea!

Instead - create your own custom object to hold all the stuff you presently want the session to hold. Make that object serializable and persist it to disk or a database with a file name or key that you can associate with the user.

Bill
 
reply
    Bookmark Topic Watch Topic
  • New Topic