wood burning stoves 2.0*
The moose likes Servlets and the fly likes How to know login & logout status of user Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to know login & logout status of user " Watch "How to know login & logout status of user " New topic
Author

How to know login & logout status of user

Abhisar Singh
Greenhorn

Joined: Aug 02, 2006
Posts: 16
Hi,

I am new to "J2EE". I am codeing a simple web application. In this application i am providing the functionality of shopping cart..NOW the problem is that I want
to check weather user is LOGEDIN or not before user adds any item in shopping to shopping cart... How can we do this checking... I am thinking of setting an attribute at session level say f=0 and check this attrib when user clicks on add item button if ist ZERO tell user to sign in otherwise procced with add item.. BUT session are supposed to start when user logsin...

I am a bit confused and dont know weather I was able state my problem clearly but if somebody can usnderstand it an provide a sloution I will be very thankful....
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
(1) When the user successfully logs into your application, put the userid in the session

session.setAttribute("loggedInUserId",getUserId());

(2) Create a filter and map it to *.jsp. ie., any request to any jsp page will pass through this filter.

Your filter logic should be something like:

If this is the login page, then no further check is required.
If this not the login page, check whether the 'loggedInUser' attribute exists in the session or not. If it doesn't redirect the user to the login page.



MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Abhisar Singh
Greenhorn

Joined: Aug 02, 2006
Posts: 16
hi thanks Lynette Dawson

but I dont want to force login.. a user will be able browse items without signing in as soon as he clicks on add item we need to check weather he is logged or not..

there can be two cases:
1) as soon as user access our web site first thing we ask him to sign in..and start a session...

2) OR he can browse the web site without signing in (and no session is created) but when he clicks on add item we need to check first if user is logged in or not and then procced accordingly. because first thing a user may do is to login and then procced with the shopping...but he may not do so... so how can we determine weather he is logged in or not before clicking on add item...... I hope this time I am more clear....
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
ok in that case, its just a matter of checking whether the "loggedInUser" attribute exists in the session or not (when the user attempts to put an item in the shopping car).

Abhisar Singh
Greenhorn

Joined: Aug 02, 2006
Posts: 16
Hey Lynette Dawson thanks a lot...

I knew the solution will be easy... its just that I am new to servlets & JSP and I dont know all the options available.... Thanks again...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to know login & logout status of user
 
Similar Threads
switching between cookies and url rewritten, HELP
shopping cart or shopping memo?
SLSB & SessionSynchronization interface
Shopping cart using session
How to check if session is active or not?