aspose file tools*
The moose likes Servlets and the fly likes Will tomcat creates a new session for every forward to a jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Will tomcat creates a new session for every forward to a jsp page" Watch "Will tomcat creates a new session for every forward to a jsp page" New topic
Author

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

Purushotham Honey
Greenhorn

Joined: Jul 21, 2011
Posts: 6

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?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5
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

Java Resources at www.wbrogden.com
Purushotham Honey
Greenhorn

Joined: Jul 21, 2011
Posts: 6
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Purushotham Honey
Greenhorn

Joined: Jul 21, 2011
Posts: 6
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?
Aashima Arora
Ranch Hand

Joined: Jun 15, 2010
Posts: 59

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?

Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

The container itself will create the session....
Yes the code mentioned above is fine.... You can also use
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

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

Joined: Jun 15, 2010
Posts: 59

Sorry, i meant that only
vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
Hi Anupam,
you wrote



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




Please check that...
Vipul


Thanks
Vipul Kumar
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
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 bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
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.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Will tomcat creates a new session for every forward to a jsp page
 
Similar Threads
calling JSP n times with Different Inputs stored in a Session
Doubt in JSP working with Netscape4.76?
Can we invkoke JSP on submit of a form?
getSession(boolean create) in HttpServletRequest
Session timeout