Forums Register Login
How to use session Tracking in JSP
Hi everyone,
I want to know how to use the session tracking in my JSP pages. I am presently using hidden fields with which i am able track a user. But i want to stop others (who has no User ID) to copy the URL to the page and have an access over it.
Hope you understood my problem.
Any suggestion is appreciated.
Thank you,
Hey Harish,
you can either use Cookies or Session objects to maintain session.
Session object is the preferred way.
check out HttpSession interface in javax.servlet.http package.
i can provide some examples if you need
Hi vivek and all,
Thanks for your reply. but i want to know the way of programming.
I logged into rediffmail. I got the inbox page. I copied the URL of that page and logged out from that.
I opened a new IE and pasted the URL in address bar. I got the page of invalid session. I want to make the same sort of thing in my project tooo.
How can i go with it? i am new to programming. Any help in coding is appreciated.
THank you,
normally a session is terminated when you close your browser. therefore if you try to access the same page in a newly opened browser, the site will not give you access because there is no session associated with your browser.
You can define a session in your JSP page to keep track of users.
//a new session
HttpSession session = req.getSession(true);
//get the value of session
String x = (String) session.getAttribute("Object")
if(x == null) {
// redirect the user to another page
else {
// forward the user to the requested page
THanks Amir. The thing which you gave me is working. I want to know the way to set the cookies and call them in each and every page user browses throu'
Can anybody please tell me the way to implement cookies in JSP.
Thank you,
hey Harish
cookies isnt the way you should implement session management coz the data is stored on the client side.
its a good practice to use Session objects.
lets say u have 2 jsp files, in the first jsp page u store information and in the next jsp page u want to retrieve that info.
first jsp
session.setAttribute("userid", "harish");
in jsp session is an implicit object avialable to use just like request and response.
now u have placed an object in session. u can access this object anywhere in your project.
so to get that object back in next page
second jsp
String userid = (String) session.getAttribute("userid");
note getAttribute will return an Object type, so u have to typecast it.
if u still need more info let me know
Hi Vivek,
Thanks a lot for ur response.
I think now I got a nice picture of what is to be used and when it is to be used.
I will surely contact this forum when I have any doubt. I am very happy to get a response in here.
once again thanks,
It will give me the powers of the gods. Not bad for a tiny ad:
Rocket Oven Kickstarter - from the trailboss

This thread has been viewed 5900 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 20, 2018 18:24:57.