Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

session without request

 
Arvind Subramanian
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • 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?
 
Arvind Porlekar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • 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.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65338
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
veeresh soni
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65338
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13077
6
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic