从servlet设置错误消息和正文 [英] Setting both error message and body from servlet

查看:373
本文介绍了从servlet设置错误消息和正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个servlet,它将返回如下的http响应:

I want to write a servlet that will return an http response like so:

HTTP/1.1 500 <short custom message>
Content-Length: ...

<longer custom message>

原因是我希望程序化客户端能够处理响应消息以获取特定内容响应,但我也希望用更长的解释填写响应正文,以便使用浏览器轻松命中。

The reason is that I want a programmatic client to be able to process the response message to take a particular response but I also want to fill in the response body with a longer explanation so that it's easy to hit using a browser.

现在,HttpServletResponse有一个sendError(int,String )允许我指定错误代码和消息的方法。 javadocs只说该消息将嵌入某种html页面,但没有关于设置http响应消息。调用此方法后,不允许您在响应中写入任何其他内容。在我的测试中(使用jetty),消息用于http响应和html主体,这对我来说没问题,除了我想指定两个不同的字符串,我不认为http响应的设置使用不同的实现保证了消息。

Now, HttpServletResponse has a sendError(int, String) method that allows me to specify the error code and a message. The javadocs only say that the message will be embedded in some kind of html page, but nothing about setting the http response message. After calling this method, you are not allowed to write anything else to the response. In my tests (with jetty), the message is used both for the http response and an html body, which would be fine with me, except that I want to specify two different strings and I don't think the setting of the http response message is guaranteed with a different implementation.

还有一个setStatus(int)方法,您可以使用任何代码调用,然后您可以编写自己的html主体。这很接近,除了你不能指定http响应消息。

There's also a setStatus(int) method that you can call with any code, and then you can write your own html body. This is close, except that you can't specify the http response message.

最后,有一个setStatus(int,String)方法实际上正是我想要的,但由于某种模糊性,它已被弃用。我假设一些servlet容器正在将消息写入响应主体并关闭响应。

Finally, there's a setStatus(int, String) method that actually does exactly what I want, but it's deprecated due to some kind of ambiguity. I'm assuming some servlet containers were writing the message to the response body and closing the response.

除了使用弃用的方法之外,我假设我搞砸了在这里,但我很好奇是否有其他人知道任何技巧?

Aside from using the deprecated method, I'm assuming I'm screwed here, but I'm curious if anyone else knows any tricks?

推荐答案


还有一个setStatus (int)您可以使用任何代码调用的方法,然后您可以编写自己的html主体。这很接近,除了你不能指定http响应消息。

There's also a setStatus(int) method that you can call with any code, and then you can write your own html body. This is close, except that you can't specify the http response message.

你有什么理由不使用 response.setStatus(500)设置HTTP状态代码,然后写入响应的输出流?

Is there any reason why you would not use response.setStatus(500) to set the HTTP status code, followed by writing to the response's output stream?

这是如何在最低级别实现写入响应主体。

This is how "writing to the response body" is achieved at the lowest level.

以下是一个示例:

public class ErrorServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

        // 500 error
        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        resp.getWriter().print("<html><head><title>Oops an error happened!</title></head>");
        resp.getWriter().print("<body>Something bad happened uh-oh!</body>");
        resp.getWriter().println("</html>");
    }
}

web.xml:

<web-app>
    <servlet>
        <servlet-name>myerror</servlet-name>
        <servlet-class>brown.testservlet.ErrorServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myerror</servlet-name>
        <url-pattern>/myErrorPage</url-pattern>
    </servlet-mapping>
</web-app>

输出:

$ curl -I http://localhost:8080/testservlet/myErrorPage  
HTTP/1.1 500 Internal Server Error  
Content-Length: 108  
Server: Jetty(6.1.16)  

$ curl -v http://localhost:8080/testservlet/myErrorPage
* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /testservlet/myErrorPage HTTP/1.1
> User-Agent: curl/7.20.0 (i686-pc-mingw32) libcurl/7.20.0 OpenSSL/0.9.8k zlib/1.2.3
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 500 Internal Server Error
< Content-Length: 108
< Server: Jetty(6.1.16)
<
<html><head><title>Oops an error happened!</title></head><body>Something bad happened uh-oh!</body></html>
* Connection #0 to host localhost left intact
* Closing connection #0

这篇关于从servlet设置错误消息和正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆