user3921678
user3921678

Reputation: 27

How to check whether Session Attribute is successfully set?

I have multiple servlets and jsp pages (around 100) and I want to do session management for these using HttpSession. Is it necessary to check for HttpSession.getAttribute() in each of the servlets and jsp pages after setting the attribute for HttpSession.

Is there any convenient way so that I don't have to check HttpSession.getAttribute() in each of the servlets and jsp files?

Upvotes: 0

Views: 1000

Answers (2)

bablu
bablu

Reputation: 611

I guess what you want to achieve can be done using Servlet Filters. For more info you can check this:- http://www.journaldev.com/1933/java-servlet-filter-example-tutorial

http://tutorials.jenkov.com/java-servlets/servlet-filters.html

Upvotes: 1

Bimalesh Jha
Bimalesh Jha

Reputation: 1504

If you do not want to repeat checking session in every servlet (and jsp) you can create a base servlet class having its service() method overridden with a code in the beginning like HttpSession.getAttribute() and then call an abstract doService() which you must implement in all the servlets and latter must extend from the base servlet. For JSPs, it is bit tricky (as extending from a JSP is complex, may not be even practical), but you can have a JSP fragment with this code which you can <@include ...> in all your JSPs at the beginning. Using some template framework for JSP will make it easier. Hope, this gives you some idea on how to proceed further.

Upvotes: 0

Related Questions