gpt4 book ai didi

java - 当我运行 java servlet 查看 JSON 结果时——它弹出文件下载而不是 http ://localhost:8080/

转载 作者:行者123 更新时间:2023-11-28 22:50:32 25 4
gpt4 key购买 nike

gif of file download pop up

如何在浏览器中测试 JSON 字符串,而无需执行下载 json 文件的步骤,因为当我运行 servlet 时会弹出“文件下载”对话框,然后在浏览器中查看下载的 json 文件。

下面是 postman 的屏幕截图 - postman 返回 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.
}
}

最佳答案

找到了!解决方案分为两部分,首先,您删除

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

http://localhost:8080/webData/queryreturn

关于java - 当我运行 java servlet 查看 JSON 结果时——它弹出文件下载而不是 http ://localhost:8080/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41419999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com