This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Tomcat creating Httpsessions automatically  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In one of my servlets i have written the following code

HttpSession session = request.getSession(false);

(i.e if already session is there then only give me back the session otherwise return null)

The above statement is always returning a session even though i have not created a session explicitly.It is never returning a null.

I assume Tomcat by default is creating session. Can we prevent TOmcat from creating the session automatically?

thanks
rama
 
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any JSPs in your application?
JSPs, by default create sessions automatically.

Out of curiosity, why do you want to suppress the creation of sessions?
If you're not doing anything with them, their creation
 
ramaa Vadakattuu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no JSPs are there
1) it is an intial request to the servlet.
>>Out of curiosity, why do you want to suppress the creation of sessions?
>>If you're not doing anything with them, their creation

2) Some where in the Java code iam basing my decision on this
for example
for a request
If already session is there (he is logged in user)
{
//loggedin user
//do some stuff
}
else if no session is there
{
//new user
// do some other stuff
}

the above statements in Java look like this
if(request.getSession(false) is not null)
{
//logged in User
}
else
{
//New user who has not logged in
}


As the apache is creating session automatically never i am going to else loop.

I hope your understand the above scenario.

The only this i really need to undestand is why it is creating the session automatically ? Can we prevent such automatic creation?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure when exactly they're being created but I've found it more reliable to bind an object (could be something as simple as a string) to session when a user logs in. Then check for the existence of that object in session instead of the session object itself. Then, it doesn't matter when or how the session gets created.
 
ramaa Vadakattuu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Do you have any JSPs in your application?
JSPs, by default create sessions automatically.



Sorry actually Iam moving to the servlet via index.jsp.
This index.jsp is creating the sessions automatically.
THanks for your reply.
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!