• 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

Will tomcat creates a new session for every forward to a jsp page

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want my web application to contain only one session created at login. But my server is creating a separate session for every request to a jsp page. How can i configure to have only one session in my application?
 
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
As long as the login servlet and various .jsp are in the same web application, you should only get one session shared by all.

Why are you sure that new sessions are being created? Kindly show the code.

Bill
 
Purushotham Honey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure
The following the code written in my action servlet (I have used struts 1.3)

HttpSession sess = request.getSession (true);
if (sess.isNew() == false) {
sess.invalidate();
sess = request.getSession(true);
}

I confirmed that multiple sessions has created by using a HttpSessionListener

The following is the sample code that the console generated

********About to create a session(LoginAction.execute.if)******** //generated by sys.out
--------Session Destroyed--------Remaining : 0 //generated by sys.out
--------Session Created--------Active : 1 //generated by sys.out
29486 [http-8080-1] DEBUG org.hibernate.event.def.DefaultSaveOrUpdateEventListener - saving transient instance
29486 [http-8080-1] DEBUG org.hibernate.id.IncrementGenerator - fetching initial value: select max(sid) from TESTSCHEMA.sessiondb
29486 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
29486 [http-8080-1] DEBUG org.hibernate.SQL - select max(sid) from TESTSCHEMA.sessiondb
Hibernate: select max(sid) from TESTSCHEMA.sessiondb
29486 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - preparing statement
29488 [http-8080-1] DEBUG org.hibernate.id.IncrementGenerator - first free id: 58
29488 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
29488 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - closing statement
29488 [http-8080-1] DEBUG org.hibernate.event.def.AbstractSaveEventListener - generated identifier: 58, using strategy: org.hibernate.id.IncrementGenerator
29488 [http-8080-1] DEBUG org.hibernate.event.def.AbstractSaveEventListener - saving [com.vunya.hibernate.beans.SessionDB#58]
29495 [http-8080-1] DEBUG org.hibernate.transaction.JDBCTransaction - commit
29495 [http-8080-1] DEBUG org.hibernate.impl.SessionImpl - automatically flushing session
29495 [http-8080-1] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - flushing session
29496 [http-8080-1] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - processing flush-time cascades
29496 [http-8080-1] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - dirty checking collections
29496 [http-8080-1] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Flushing entities and processing referenced collections
29498 [http-8080-1] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Processing unreferenced collections
29498 [http-8080-1] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Scheduling collection removes/(re)creates/updates
29499 [http-8080-1] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Flushed: 1 insertions, 0 updates, 0 deletions to 2 objects
29499 [http-8080-1] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
29499 [http-8080-1] DEBUG org.hibernate.pretty.Printer - listing entities:
29499 [http-8080-1] DEBUG org.hibernate.pretty.Printer - com.vunya.hibernate.beans.SessionDB{sid=58, logintime=2011-07-21 23:48:27, sessionId=ABF61476D191C40B77B8AF570ABB74EA, userName=agu1, password=agp1}
29499 [http-8080-1] DEBUG org.hibernate.pretty.Printer - com.vunya.hibernate.beans.LoginCheck{username=agu1, userid=1, type=1, password=agp1}
29499 [http-8080-1] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - executing flush
29499 [http-8080-1] DEBUG org.hibernate.jdbc.ConnectionManager - registering flush begin
29499 [http-8080-1] DEBUG org.hibernate.persister.entity.AbstractEntityPersister - Inserting entity: [com.vunya.hibernate.beans.SessionDB#58]
29504 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
29504 [http-8080-1] DEBUG org.hibernate.SQL - insert into TESTSCHEMA.sessiondb (sessionid, username, password, logintime, sid) values (?, ?, ?, ?, ?)
Hibernate: insert into TESTSCHEMA.sessiondb (sessionid, username, password, logintime, sid) values (?, ?, ?, ?, ?)
29504 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - preparing statement
29505 [http-8080-1] DEBUG org.hibernate.persister.entity.AbstractEntityPersister - Dehydrating entity: [com.vunya.hibernate.beans.SessionDB#58]
29505 [http-8080-1] DEBUG org.hibernate.type.StringType - binding 'ABF61476D191C40B77B8AF570ABB74EA' to parameter: 1
29505 [http-8080-1] DEBUG org.hibernate.type.StringType - binding 'agu1' to parameter: 2
29505 [http-8080-1] DEBUG org.hibernate.type.StringType - binding 'agp1' to parameter: 3
29505 [http-8080-1] DEBUG org.hibernate.type.TimestampType - binding '2011-07-21 23:48:27' to parameter: 4
29506 [http-8080-1] DEBUG org.hibernate.type.IntegerType - binding '58' to parameter: 5
29509 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
29509 [http-8080-1] DEBUG org.hibernate.jdbc.AbstractBatcher - closing statement
29509 [http-8080-1] DEBUG org.hibernate.jdbc.ConnectionManager - registering flush end
29510 [http-8080-1] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - post flush
29510 [http-8080-1] DEBUG org.hibernate.jdbc.JDBCContext - before transaction completion
29510 [http-8080-1] DEBUG org.hibernate.impl.SessionImpl - before transaction completion
29512 [http-8080-1] DEBUG org.hibernate.transaction.JDBCTransaction - committed JDBC Connection
29512 [http-8080-1] DEBUG org.hibernate.jdbc.JDBCContext - after transaction completion
29512 [http-8080-1] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
29512 [http-8080-1] DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
29513 [http-8080-1] DEBUG org.hibernate.connection.DriverManagerConnectionProvider - returning connection to pool, pool size: 1
29513 [http-8080-1] DEBUG org.hibernate.impl.SessionImpl - after transaction completion
29513 [http-8080-1] INFO LOG - Logged in as Agent
29513 [http-8080-1] INFO LOG - User name : agu1
29513 [http-8080-1] INFO LOG - Password : agp1
29513 [http-8080-1] INFO LOG - Session ID : ABF61476D191C40B77B8AF570ABB74EA
29513 [http-8080-1] DEBUG org.apache.struts.tiles.TilesRequestProcessor - processForwardConfig(/Agent/dashboard.jsp, false)
29514 [http-8080-1] DEBUG org.apache.struts.tiles.TilesRequestProcessor - '/Agent/dashboard.jsp' - processed as uri
29514 [http-8080-1] DEBUG org.apache.struts.action.RequestProcessor - processForwardConfig(ForwardConfig[name=success1,path=/Agent/dashboard.jsp,redirect=true,contextRelative=false,module=null])
29528 [http-8080-2] DEBUG org.apache.struts.util.PropertyMessageResources - Initializing, config='org.apache.struts.taglib.html.LocalStrings', returnNull=true
--------Session Created--------Active : 2
--------Session Created--------Active : 3
--------Session Created--------Active : 4
.........


Please can you help in resolving my problem
 
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
Why are you bothering with isNew() and invalidation at all? That's a recipe for FAIL.

Let the JSP engine handle the session and you just use the session. Don't test the session itself. but test for things that you put into the session.
 
Purushotham Honey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya but the code is written in the action class for the login page. My intention is to have a new session created (not to use the previous session) when a user login. Is the code relevant for my requirement?
 
Ranch Hand
Posts: 59
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want to create a session at login,

JSP by default has a session object set to NULL
if you want to create a session at login
use

session = request.getSession();

suppose there is member page you are redirecting to

in member page you can code



is that what you want?

 
Ranch Hand
Posts: 42
Firefox Browser Tomcat Server Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The container itself will create the session....
Yes the code mentioned above is fine.... You can also use
 
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

Aashima Arora wrote:You want to create a session at login,


No, you don't. You want to let the container create the sessions. At login, place a scoped variable into the session. And that is what you should be checking for.
 
Aashima Arora
Ranch Hand
Posts: 59
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, i meant that only
 
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anupam,
you wrote



Here "sess" variable raises NULLPointerException as sess=null.




Please check that...
Vipul
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Purushotham Honey wrote:Ya but the code is written in the action class for the login page. My intention is to have a new session created (not to use the previous session) when a user login. Is the code relevant for my requirement?



Assuming that your scenario is , a login.jsp is showing the form and its action is the action class;
The Jsp by default has page attribute session set to true so there is already a session object in the application. why wouldn't you use that in your action?
 
vipul John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Purushotham,

May be this code satisifes your requirement.
After login is successful


Now a session has been created.
For further requests, you just use the below code.



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic