wood burning stoves 2.0*
The moose likes Servlets and the fly likes session and exception handling in jsp and servlet? 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 "session and exception handling in jsp and servlet?" Watch "session and exception handling in jsp and servlet?" New topic
Author

session and exception handling in jsp and servlet?

Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2049
In creating a new application, what considerations do we do when designing the jsp/servlet application, in relation to session and exceptions?

e.g.
- at each beginning of jsp, always check if session exists? if it is required?
- for exception in servlet or jsp that are not business errors, do we use request parameters and redirect/dispatch into an error jsp?

I have built my first application at work but not sure on these 2 issues of how I want to maintain/control my session, and the exception occurrence.

For exceptions, I simply set error variables into the session and dispatch into a common error jsp. For session, I throw an exception when it is null and it is not expected to be null at that point.
[ October 07, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Avoid having to put goo on each and every JSP page by employing a filter.

Define an error handler in web.xml and write a centralized servlet to handle errors in a consistent fashion.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Now so many good and trusted MVC frameworks are available that developers do not go for making a application with straight jsp's and servlets. All major MVC frameworks (like struts) provide a global exception handling framework.That will take care of the exceptions from the action classes (in case of struts).
In case you want to make a small application using servlets and jsp's ,you might configure error pages in web. xml for various error codes and exception types so that in case of exception the exception specific page would be displayed.


Rahul Bhattacharjee
LinkedIn - Blog
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Originally posted by Rahul Bhattacharjee:
Now so many good and trusted MVC frameworks are available that developers do not go for making a application with straight jsp's and servlets


I strongly beg to differ.

Please do not speak for all developers. Many experienced developers I know eschew the big and "popular" frameworks.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2049
Originally posted by Bear Bibeault:
Avoid having to put goo on each and every JSP page by employing a filter.

Define an error handler in web.xml and write a centralized servlet to handle errors in a consistent fashion.


Thanks, I missed that part, using error component in the web xml.

I used redirect to an error page, on all components. If I have time, I will fix the design.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

The error pages in the web.xml file are fantastic.

Also, the ability to map specific exceptions to a particular error page is great. You can make your own exceptions, and then map them to specific error pages.

Filters are best to use for tracking sessions, and not only sessions, but that certain objects are inside of the session.

When developing applicatoins, I usually put together a state transition table, and try to isolate a maximum of five potential state transitions. I then create filters that identify whether a person 'should' be in a particular state, and apply those to the appropriate Servlet and JSPs.

State management should never be taken lightly.

-Cameron McKenzie
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9913
    
158

When developing applicatoins, I usually put together a state transition table, and try to isolate a maximum of five potential state transitions. I then create filters that identify whether a person 'should' be in a particular state, and apply those to the appropriate Servlet and JSPs.

State management should never be taken lightly.


Was overhearing the converstaion going on in this thread. I would be interested in knowing what state management and state transition is. Can you please provide a simple example as to what state management is?


[My Blog] [JavaRanch Journal]
 
wood burning stoves
 
subject: session and exception handling in jsp and servlet?
 
Similar Threads
how to access a class in servlet context from JSP
Location of Compiled JSP source in WebLogic 7.0
Disadvantages of servlet?
Questions about Exception handling
HashMap