当我 运行 java servlet 查看 JSON 结果时——它弹出文件下载而不是 http://localhost:8080/
When I run java servlet to view JSON result -- it pops up file download instead of http://localhost:8080/
gif of file download pop up
如何在浏览器中测试 JSON 字符串,而无需执行下载 json 文件的步骤,因为当我 运行 servlet 时会弹出文件下载对话框,然后在浏览器中查看下载的 json 文件。
下面是postman的截图-邮递员[=34=]一个404
postman on localhost 8080
下面是web.xml映射
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<!-- General Description of the web application -->
<display-name>webData</display-name>
<description>data managed in web data table grid</description>
<!-- For directory request -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Define servlets -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>queryreturn</servlet-name>
<servlet-class>com.queryData.Return.QueryReturn</servlet-class>
</servlet>
<!-- Note: All <servlet> elements MUST be grouped together and
placed IN FRONT of the <servlet-mapping> elements -->
<!-- Define servlet's URL mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>queryreturn</servlet-name>
<url-pattern>/queryreturn</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
下面是成功生成 JSON 字符串但不会在浏览器中 运行 的 bean
package com.queryData.Return;
//Import required java libraries
import java.io.*;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import org.json.JSONObject;
import com.queryData.main.Main;
// Extend HttpServlet class
public class QueryReturn extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void init() throws ServletException
{
// Do required initialization
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
Main m = new Main();
List<JSONObject> jObj = m.getJsonObject();
StringBuilder sb = new StringBuilder();
for(int i =0 ; i < jObj.size(); i++)
{
sb.append(jObj.get(i).toString());
}
String responseStr = "{\"data\":[" + sb + "]}";
// Set response content type
response.setContentType("application/json");
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println(responseStr);
}
public void destroy()
{
// do nothing.
}
}
Internet Explorer 将“application/json
”作为文本文件处理。您的代码没有问题,只是由于您的浏览器造成的麻烦。
这可能有帮助:What problems may using the MIME type application/json cause?
您可以在 Chrome、Firebox 和 Safari 中使用 Advanced Rest Client 插件
https://github.com/wiztools/rest-client, https://insomnia.rest/ 对于 Windows
postman报错的原因是uri不正确。看起来你错过了应用程序名称。请试试这个“http://localhost:8080/webData/queryreturn”
'download' 问题的原因可能是内容类型不正确。请检查您在实际代码中使用了正确的类型。在这里,看起来不错。
找到了!解决方案分为两部分,首先,删除
response.setContentType("application/json;charset=UTF-8");
来自 servlet,它运行完美并显示在 Eclipse 中。
其次,URL 不正确,我找到了这篇令人难以置信的文章 - 必读 - 新加坡南洋理工大学
article regarding the absolute URL for this servlet INCLUDES the Project name
就是这样,
(1) 去掉setContentType
和
(2) 使用绝对 URL WITH 项目名称
http://localhost:8080/webData/queryreturn
我正在阅读你的问题和答案。我认为这种行为只是 IE 问题。请检查相同的代码打开 Google Chrome、Mozilla、Opera 或其他。你的代码很好。或者您可以将内容类型更改为 "text/plain" 并检查它。
您可以尝试使用 Spring 来构建健壮且可扩展的 REST 解决方案,Servlet 很好(对于简单的事情)。
希望你能收到。问候!
gif of file download pop up
如何在浏览器中测试 JSON 字符串,而无需执行下载 json 文件的步骤,因为当我 运行 servlet 时会弹出文件下载对话框,然后在浏览器中查看下载的 json 文件。
下面是postman的截图-邮递员[=34=]一个404
postman on localhost 8080
下面是web.xml映射
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<!-- General Description of the web application -->
<display-name>webData</display-name>
<description>data managed in web data table grid</description>
<!-- For directory request -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Define servlets -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>queryreturn</servlet-name>
<servlet-class>com.queryData.Return.QueryReturn</servlet-class>
</servlet>
<!-- Note: All <servlet> elements MUST be grouped together and
placed IN FRONT of the <servlet-mapping> elements -->
<!-- Define servlet's URL mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>queryreturn</servlet-name>
<url-pattern>/queryreturn</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
下面是成功生成 JSON 字符串但不会在浏览器中 运行 的 bean
package com.queryData.Return;
//Import required java libraries
import java.io.*;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import org.json.JSONObject;
import com.queryData.main.Main;
// Extend HttpServlet class
public class QueryReturn extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void init() throws ServletException
{
// Do required initialization
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
Main m = new Main();
List<JSONObject> jObj = m.getJsonObject();
StringBuilder sb = new StringBuilder();
for(int i =0 ; i < jObj.size(); i++)
{
sb.append(jObj.get(i).toString());
}
String responseStr = "{\"data\":[" + sb + "]}";
// Set response content type
response.setContentType("application/json");
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println(responseStr);
}
public void destroy()
{
// do nothing.
}
}
Internet Explorer 将“application/json
”作为文本文件处理。您的代码没有问题,只是由于您的浏览器造成的麻烦。
这可能有帮助:What problems may using the MIME type application/json cause?
您可以在 Chrome、Firebox 和 Safari 中使用 Advanced Rest Client 插件 https://github.com/wiztools/rest-client, https://insomnia.rest/ 对于 Windows
postman报错的原因是uri不正确。看起来你错过了应用程序名称。请试试这个“http://localhost:8080/webData/queryreturn”
'download' 问题的原因可能是内容类型不正确。请检查您在实际代码中使用了正确的类型。在这里,看起来不错。
找到了!解决方案分为两部分,首先,删除
response.setContentType("application/json;charset=UTF-8");
来自 servlet,它运行完美并显示在 Eclipse 中。 其次,URL 不正确,我找到了这篇令人难以置信的文章 - 必读 - 新加坡南洋理工大学
article regarding the absolute URL for this servlet INCLUDES the Project name
就是这样, (1) 去掉setContentType 和 (2) 使用绝对 URL WITH 项目名称
http://localhost:8080/webData/queryreturn
我正在阅读你的问题和答案。我认为这种行为只是 IE 问题。请检查相同的代码打开 Google Chrome、Mozilla、Opera 或其他。你的代码很好。或者您可以将内容类型更改为 "text/plain" 并检查它。
您可以尝试使用 Spring 来构建健壮且可扩展的 REST 解决方案,Servlet 很好(对于简单的事情)。
希望你能收到。问候!