This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Optimizing Session tracking code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Optimizing Session tracking code." Watch "Optimizing Session tracking code." New topic
Author

Optimizing Session tracking code.

ellie spring
Greenhorn

Joined: Jun 15, 2011
Posts: 24

Hi ranchers,

I am working on simple web app using servlet,jsp and el in a MVC pattern approach.
I have a code that tracks current user session in one of my servlets.

I have completed my goal to redirect those users who are not login to the page that tell them to login.
here is the example servlet



What i want is to have the same behavior for those pages that are intended for authenticated users like ones who are login to my website.
Is there a way to separate this code in another class and just call the methods to check if a session is null? or just copy pase this code to all of my servlet.

Thanks in advance.


"Nothing is impossible, impossible takes a little longer".
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

Yes.. there is a much better way to do what you want.
You can use HTTP Form Based Authentication, If you want to use any framework for this, i would recommend Spring Security...


Regards, Prasad
SCJP 5 (93%)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60743
    
  65

Or, if you want to roll your own, look into the use of a filter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ellie spring
Greenhorn

Joined: Jun 15, 2011
Posts: 24

Thanks for the replies.

I think will learn to use filters.
Save the Spring framework for the future.
I'm just starting to learn the fundamentals.

Are there any tutorials or articles about filters where i can learn?.

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60743
    
  65

Servlet filters
ellie spring
Greenhorn

Joined: Jun 15, 2011
Posts: 24
Thank you sir.

Its working now.
Servlet Filters are quite powerful.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Optimizing Session tracking code.
 
Similar Threads
Session does not persist
Checking if session exists problem
Problems with servlet filter
Session Data Crossover Problem... Please help... Thanks!
Session Data Crossover Problem