tags:

views:

171

answers:

1

I use the following code to catch the SQL Expression, when i try to delete a primary key wich is also a foreign key and is mapped to another table, my question is, how do you forward this to a common error page ?

To catch the expression,

if(e.Exception is SqlException)
        {
             SqlException ex = (SqlException)e.Exception; 
             if (ex.Number == 547)
                ErrorMessage("Record cannot be deleted or changed " + 
                               "as it is being used somewhere else");

             else if (ex.Number == 2627)
                ErrorMessage("Record cannot be saved, as another " + 
                             "record with this key already exists");

             else
                ErrorMessage(ex.Message.ToString());
        }
        else
             ErrorMessage("System Error :"+e.Exception.Message.ToString());
A: 

It all depends how you are doing your JSPs.

There are two fundamentally different approaches, referred to as Model 1 and Model 2. I used Model 2, so requests hit a servlet, the servlet does some business logic and then decides which page to display.

public class MyServlet extends HttpServlet {

   public void doPost (HttpServletRequest req, HttpServletResponse res){

          // some work here

          ServletContext sc = getServletContext();

          if ( good ){
                   session.putValue("beanToDisplay", anAnswer);
                   String url="/jsp/Good.jsp";
                   RequestDispatcher rd = sc.getRequestDispatcher(url);
                   rd.forward(req, res);
          } else {
                   session.putValue("exceptionToDisplay", anException);
                   String url="/jsp/bad.jsp";
                   RequestDispatcher rd = sc.getRequestDispatcher(url);
                   rd.forward(req, res);
          }

   }
djna