How to found out url with which the request arrived at error handler?

萝らか妹 提交于 2019-12-10 10:42:43

问题


I send following http request:

http://localhost:8081/member/createCompany/getSmallThumbnail/

On server side I hit into controller method:

@RequestMapping("/error")
public String error(Model model, HttpServletRequest request){
    if(request.getRequestURI().contains("thumbnail")){
        System.out.println("thumbnail accepted");
     }
     request.toString();
     model.addAttribute("message", "page not found");
     return "errorPage";
}

At this method I want to know url with which the request arrived.

If in debug I stop inside this method I see information needed for me:

But I cannot find method in request which will return this.

Please help to return url which I want.

P.S.

Actually I have not mapped controller in my spring mvc application(url is broken) for http://localhost:8081/member/createCompany/getSmallThumbnail/. This url("/error") configured in web.xml as error page.


回答1:


Your request got redispatched to /error (presumably for error processing).

If this framework follows the normal Servlet error dispatching behavior, then your original request can be found in the HttpServletRequest.getAttributes() under the various javax.servlet.RequestDispatcher.ERROR_* keys.

  • ERROR_EXCEPTION - The exception object
  • ERROR_EXCEPTION_TYPE - The type of exception object
  • ERROR_MESSAGE - the exception message
  • ERROR_REQUEST_URI - the original request uri that caused the error dispatch
  • ERROR_SERVLET_NAME - the name of the servlet that caused the error
  • ERROR_STATUS_CODE - the response status code determined for this error dispatch

What you want is

String originalUri = (String) request.getAttribute(
                                       RequestDispatcher.ERROR_REQUEST_URI)


来源:https://stackoverflow.com/questions/32910492/how-to-found-out-url-with-which-the-request-arrived-at-error-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!