GeeCON Prague 2014*
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


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: 532

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...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

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: 61314
    
  66

Servlet filters
ellie spring
Greenhorn

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

Its working now.
Servlet Filters are quite powerful.

 
GeeCON Prague 2014
 
subject: Optimizing Session tracking code.