Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use session Tracking in JSP  RSS feed

 
Harish Kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Harish.
 
Vivek Mongolu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Vivek
 
Harish Kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Harish.
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harish,
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
}
HTH
 
Harish Kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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,
Harish.
 
Vivek Mongolu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Vivek
 
Harish Kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Harish.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!