Forums Register Login

session without request

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
try with putting the object into the ServletContext...!!!

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

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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
Good heavens! What have you done! Here, try to fix it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 8211 times.
Similar Threads
Question about page scope and implicit object
how to get Context information inside a class?
session null pointer exception
who to get the current session in my web app
Related to Session and Request objects
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:35:22.