• 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

How to use session Tracking in JSP

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic