纯文本 JSP 响应
我被这个问题困住了。答案可能很简单,但对于我的一生来说,我无法弄清楚。
这是我非常简单的 jsp 文件:
<%@page contentType="text/plain"
import="java.util.*"
import="subscriberapi.SubscriberAPI"
pageEncoding="UTF-8"%>
<%!private String Check(String jsonText)
{
SubscriberAPI subscriberAPI = new SubscriberAPI();
return subscriberAPI.Check(jsonText);
}%>
<%response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("pragma","no-cache");%>
<%=Check(request.getParameter("jsonText"))%>
我希望输出为:
{"Status":true,"Message":"All good"}
但我得到的输出是:
<html>
<head></head>
<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;">{"Status":true,"Message":"All good"}</pre>
</body>
</html>
它在浏览器中看起来正确,但实际响应转到另一个不需要 html 的进程。
如何摆脱围绕响应构建的 html? 这是 Glassfish 设置的吗?
I'm stuck on this problem. The answer might be straight forward, but for the life of me, I can't figure it out.
Here is my very simple jsp file:
<%@page contentType="text/plain"
import="java.util.*"
import="subscriberapi.SubscriberAPI"
pageEncoding="UTF-8"%>
<%!private String Check(String jsonText)
{
SubscriberAPI subscriberAPI = new SubscriberAPI();
return subscriberAPI.Check(jsonText);
}%>
<%response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("pragma","no-cache");%>
<%=Check(request.getParameter("jsonText"))%>
I would like the output to be:
{"Status":true,"Message":"All good"}
But the output I get is:
<html>
<head></head>
<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;">{"Status":true,"Message":"All good"}</pre>
</body>
</html>
It looks correct in a browser but the actual response goes to another process, that doesn't want the html.
How do I get rid of the html build around the response?
Is this set by Glassfish?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
:blush: 史诗般的失败!
当 Google Chrome 呈现 text/plain 时,它会将其包装在 html 中(如上所述)。我感觉自己像个菜鸟开发者。我应该在其他浏览器上检查它,或者编写一个测试应用程序。无论如何,我遵循了 BalusC 的建议并制作了一个 Servlet
:blush: Epic fail!
When Google Chrome is presented with text/plain it will wrap it in html (as above). I feel like such a noob developer. I should have checked it on other browsers and or wrote a test app. In any case, I followed BalusC advice and made a Servlet
您使用什么网络容器?在 Apache Tomcat 6.24 中尝试过这个。我更改了您的方法以返回硬编码的 json 字符串。输出周围没有 html 标记,工作正常。
检查您的请求参数并查看您的 SubscriberAPI::Check 方法正在执行的操作
What web container are you using? tried this in Apache Tomcat 6.24. I changed your method to return a hard-coded json string. worked OK with no html markup around output.
Inspect your request parameter and review what your SubscriberAPI::Check method is doing