R
Rob
Hello, I'm looking for an easier way to handle errors in a
servlet and pass a meaningful String to a JSP.
Here's my current setup:
public void myServlet extends HttpServlet throws
ServletException{
public void showError(HttpServletRequest req,
HttpServletResponse resp, String errorMessage){
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("errorPage.jsp");
if(dispatcher != null){
req.setAttribute("error",errorMessage);
try{
dispatcher.forward(req,resp);
}catch(ServletException se){}
catch(IOException ioe){}
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{
try{
do_something();
}catch (SomeException se){
showError(request, response, "Some Error occured");
}
}
If do_something() causes an exception, I can catch it and
forward to the JSP directy. But what do I do if do_something()
calls method2() which in turn calls method3()? I'd like
method3() to be able to forward the error to the errorPage, so
I'm passing the HttpServletRequest and HttpServletResponse
variables, obtained in doPos(), from method to method. This is
becoming a pain. There must be an easier way to grab an
exception in any method and have a clean way to show an error in
errorPage.jsp.
This is for a personal project.
TIA,
Rob
servlet and pass a meaningful String to a JSP.
Here's my current setup:
public void myServlet extends HttpServlet throws
ServletException{
public void showError(HttpServletRequest req,
HttpServletResponse resp, String errorMessage){
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("errorPage.jsp");
if(dispatcher != null){
req.setAttribute("error",errorMessage);
try{
dispatcher.forward(req,resp);
}catch(ServletException se){}
catch(IOException ioe){}
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{
try{
do_something();
}catch (SomeException se){
showError(request, response, "Some Error occured");
}
}
If do_something() causes an exception, I can catch it and
forward to the JSP directy. But what do I do if do_something()
calls method2() which in turn calls method3()? I'd like
method3() to be able to forward the error to the errorPage, so
I'm passing the HttpServletRequest and HttpServletResponse
variables, obtained in doPos(), from method to method. This is
becoming a pain. There must be an easier way to grab an
exception in any method and have a clean way to show an error in
errorPage.jsp.
This is for a personal project.
TIA,
Rob