Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 804
Radu Stoenescu Avatar asked Dec 22 '22 12:12

Radu Stoenescu


1 Answers

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.

like image 133
Abdullah Jibaly Avatar answered Apr 29 '23 11:04

Abdullah Jibaly