how can i retrieve the msg from HttpServletResponse.sendError

只谈情不闲聊 提交于 2019-12-24 07:09:08

问题


i have two tomcat servers that communicate between them. upon an error at one of the servers i would like to send an error response to the other server.

i am sending the error using: resp.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());

i am catching the response with org.apache.commons.httpclient.httpMethod.

my question is how can i retrieve the e.getMessage () that i am adding to the error message?

thanks


回答1:


You can override the page that is sent by declaring a specific page for the status code in the web.xml:

<error-page>
     <error-code>400</error-code>
     <location>/errorMsg.jsp</location>
</error-page>

In the JSP, do something like:

<%@page isErrorPage="true"%>
<%= exception.getMessage(); %>

Then, all that is sent back is the message.




回答2:


I think the error message gets packaged up in HTML and then sent, so in order to get the message you will need to parse the HTML.

I would suggest this isn't the most efficient way of transferring information between two servers. Provided that no humans need to look at the data, why don't you send the messages in a machine-readable format such as XML instead? Is there a specific reason you need to use response.sendError()?



来源:https://stackoverflow.com/questions/1782517/how-can-i-retrieve-the-msg-from-httpservletresponse-senderror

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