Tomcat does not show error message when using sendError from servlet

Ok, so I have a pretty simple webapp using a servlet, and in some cases I send and return a message to the client, for example:

response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Did not specify parameter xyz"); 

This works fine, but Tomcat (6.0.33 and Java 1.6.0_26-b03) does not show this error message above.

If I run the application on another container, for example, in the form of a glass fish, this message is displayed.

So, an example output ...

 Tomcat: 400 - Bad Request Glassfish: 400 - Did not specify parameter xyz 

Can tomcat be configured in the same way?

+6
source share
3 answers

Ok, after some additional copying, I found a solution here: How to send an HTTP message to a client correctly

You need to install:

 org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER = true 

in / conf / catalina.properties

This causes tomcat to send the error message indicated in the headers "correctly" :)

+8
source

Perhaps you would like to create a custom error page, for example 400.jsp, and put it in web.xml as

 <error-page> <error-code>400</error-code> .. . path to the page (sorry I don't remember the exact syntax) </error-page> 

On this error page, you can receive this message and show it to the user. The benefits of custom error pages are that you can apply your own CSS styles, your site layout, etc.

UPD: if you don't like custom error pages: we used this code in the RESTful API to send a user response with an error and message

 return javax.ws.rs.core.Response.status(Status.BAD_REQUEST).entity("My message here).build(); 
0
source

Tomcat no longer supports the USE_CUSTOM_STATUS_MSG_IN_HEADER property .

List of changes version 8.5.0 :

RFC 7230 states that clients should ignore the reason phrase in HTTP / 1.1 response messages. Since the reason for the phrase is optional, Tomcat no longer sends it. As a result, the system property org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER is no longer used and has been deleted. (Markt)

0
source

Source: https://habr.com/ru/post/901753/


All Articles