我想从Servlet中向HttpServletResponse添加一些纯文本内容和两个标头 [英] I want from a servlet to add some plain text content and two headers to the HttpServletResponse
问题描述
我想在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.
出什么问题了?
推荐答案
尝试首先设置标题.另外,我假设您在回复完毕后致电writer.flush()
.
Try setting your headers first. Also I'm assuming you call writer.flush()
after you are done with your response.
更新
您可以检查以下各项是否有效:
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();
}
使用curl -i http://yourapp.appspot.com
验证标题.
这篇关于我想从Servlet中向HttpServletResponse添加一些纯文本内容和两个标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!