开发者

I want from a servlet to add some plain text content and two headers to the HttpServletResponse

开发者 https://www.devze.com 2023-03-16 01:06 出处:网络
I want to add some plain text and two headers to the HttpServletResponse, the code is the following: resp.setContentType(\"text/plain\");

I want to add some plain text and two headers to the HttpServletResponse, the code is the following:

resp.setContentType("text/plain");

resp.getWriter().write(messages.get(next).getContent());

resp.addHeader("success", "yes");

resp.addHeader("hasnext", ((Boolean)hasNext).toString());

The problem I开发者_开发技巧 encounter is that sending the content prevents the sending of the headers. If I don't write the content the headers are received fine, if I include the text they don't.

What is the problem ?


Try setting your headers first. Also I'm assuming you call writer.flush() after you are done with your response.

UPDATE

Can you check if the following works:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setHeader("Content-Type", "text/plain");
    response.setHeader("success", "yes");
    PrintWriter writer = response.getWriter();
    writer.write("hello\n");
    writer.close();
}

Use curl -i http://yourapp.appspot.com to verify the headers.

0

精彩评论

暂无评论...
验证码 换一张
取 消