Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Best Approach for Session Check in each JSP page?  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I am working on a web application.My Seneraio is before displaying any JSP page.I need to check a variable in the session.


What I am think is 2 approach for this.
1)Make a Common Script file logincheck.jsp and check there using code <c:if> or <c:when> tags.Include that file in start of each page using <jsp:include></jsp:include> so this code will automatically check for those values.
2)Manually write Session code check in start of each JSP file

What will be best approach for this task between these 2 or any else?


Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both approaches are poor. Your best bet is to use a filter.

Besides, you never want to check anything on a per-page basis -- you want to check on each request, before the controller executes' not after the controller forwards to its JSP page.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Besides, you never want to check anything on a per-page basis -- you want to check on each request, before the controller executes' not after the controller forwards to its JSP page.



so do I need to check for session in my controller not in jsp?
What If I am redirecting JSP to another JSP without any controller between them?



Both approaches are poor. Your best bet is to use a filter.


I don't understand this line.Can I add cross cutting functionality for this?
Suppose I have 100pages(JSP) and 100 controller(If not using any framework) then I need to write session check code 100 times

Is in't?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:
so do I need to check for session in my controller not in jsp?

A filter.

What If I am redirecting JSP to another JSP without any controller between them?

Don't do that. But a filter handles that case.

Suppose I have 100pages(JSP) and 100 controller(If not using any framework) then I need to write session check code 100 times

No. Use a filter.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear

Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!