Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:08:06+00:00 2026-06-18T07:08:06+00:00

Is there a way to handle all possible error codes while still passing the

  • 0

Is there a way to handle all possible error codes while still passing the code to my .jsp? Here I have a single error-page passing 404 where it is added to the model. Rather than adding an error-page for every possible error code, is there a better way I can catch the error and pass the code to the controller/jsp file?

Controller

@RequestMapping(value="/error/{code}", method=RequestMethod.GET)
    public String error(@PathVariable("code") String code, Model model)
    {
        model.addAttribute("code", code);
        return "error";
    }

web.xml

<error-page>
    <error-code>404</error-code>
    <location>/error/404</location>
</error-page>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:08:07+00:00Added an answer on June 18, 2026 at 7:08 am

    You could register a generic exception resolver in Spring to catch all exceptions and transform into a rendering of your error.jsp.

    Using a specialised RuntimeException thrown by your business logic, that has a code member:

    public class MyException extends RuntimeException {
        private final Integer errorCode;
    
        public MyException(String message, Throwable cause, int errorCode) {
            super(message, cause);
            this.errorCode = errorCode;
        }
    }
    

    Or rely on existing RuntimeException instances with your code in the exception message.

    Extract the code and/or message and set the HttpServletResponse status and ModelAndView accordingly.

    For example:

    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.DispatcherServlet;
    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.ModelAndView;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @Component(DispatcherServlet.HANDLER_EXCEPTION_RESOLVER_BEAN_NAME)
    public class GenericHandlerExceptionResolver implements HandlerExceptionResolver {
    
        @Override
        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
            ModelAndView mav = new ModelAndView("error");
    
            if (e instanceof MyException) {
                MyException myException = (MyException) e;
                String code = myException.getCode();
    
                // could set the HTTP Status code
                response.setStatus(HttpServletResponse.XXX);
    
                // and add to the model
                mav.addObject("code", code);
            } // catch other Exception types and convert into your error page if required
    
            return mav;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a console application, is there any way I can handle the
Is there a way to handle the application_start event without using the global.asax? The
Is there any way to handle the exception if regex.Matches didn't match anything and
Is there a way to handle multiple running connections to different hubs from inside
Is there a better way to handle my FindAdjacent() function for my A Star
I am wondering if there is a way to handle this more elegantly. After
Is there a way to get the window handle (IntPtr) for a window after
Is there any way to queue up document list API requests and handle them
Is there a better way in the new ASP.net MVC 4 WebApi to handle
Is there a standard or common way in C++ to handle static strings that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.