简体   繁体   English

当我运行 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弹出文件下载的gif

How can I test the JSON string in the browser without going through the steps of downloading the json file as the File Download dialog pops up when I run the servlet, then viewing the downloaded json file in the browser.如何在浏览器中测试 JSON 字符串,而无需下载 json 文件,因为运行 servlet 时会弹出“文件下载”对话框,然后在浏览器中查看下载的 json 文件。

below is the screen shot of postman - postman returns a 404下面是邮递员的屏幕截图 - 邮递员返回 404

postman on localhost 8080本地主机 8080 上的邮递员

below is the web.xml for mapping下面是用于映射的 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>

below is the bean that generates the JSON string successfully but won't run in the browser下面是成功生成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.
      }
}

Found!成立! The solution is in two parts, first, you remove the解决方案分为两部分,首先,您删除

response.setContentType("application/json;charset=UTF-8");

from the servlet, and it runs PERFECT and displays within Eclipse.来自 servlet,它运行 PERFECT 并在 Eclipse 中显示。 Second, the URL was incorrect, I located this INCREDIBLE article - MUST READ - Nanyang Technological University, Singapore其次,网址不正确,我找到了这篇难以置信的文章 - 必读 - 新加坡南洋理工大学

article regarding the absolute URL for this servlet INCLUDES the Project name 关于此 servlet 的绝对 URL 的文章包括项目名称

That is it, (1) remove the setContentType AND (2) use the absolute URL WITH the Project name就是这样,(1) 删除 setContentType AND (2) 使用带有项目名称的绝对 URL

http://localhost:8080/webData/queryreturn

I was reading your question and the answers.我正在阅读您的问题和答案。 I think that behaviour is only IE issue.我认为这种行为只是 IE 问题。 Please checkout the same code opening Google Chrome, Mozilla, Opera or another.请签出打开 Google Chrome、Mozilla、Opera 或其他的相同代码。 Your code is well.你的代码很好。 Or maybe you could change the content type to "text/plain" and check it.或者,也许您可​​以将内容类型更改为“文本/纯文本”并进行检查。

You can try with Spring for make robust and scalable REST solution, Servlets are good (for simple things).您可以尝试使用 Spring 来制作健壮且可扩展的 REST 解决方案,Servlet 很好(对于简单的事情)。

I hope you will got it.我希望你会得到它。 Regards!问候!

Internet Explorer handles " application/json " as a text file. Internet Explorer 将“ application/json ”作为文本文件处理。 There is no issue with your code, just nuisance thanks to your browser.您的代码没有问题,只是由于您的浏览器造成的麻烦。

This might help: What problems may using the MIME type application/json cause?这可能会有所帮助: 使用 MIME 类型 application/json 可能会导致什么问题?

您可以在 Chrome、Firebox 和 Safari 中使用 Advanced Rest Client 插件https://github.com/wiztools/rest-clienthttps://insomnia.rest/ for Windows

The reason for the error in postman is due to incorrect uri. postman报错的原因是uri不正确。 Looks like you missed the application name.看起来您错过了应用程序名称。 Please try this " http://localhost:8080/webData/queryreturn "请试试这个“ http://localhost:8080/webData/queryreturn

The reason for the 'download' issue may be due to incorrect contentType. “下载”问题的原因可能是由于内容类型不正确。 Please check you have correct type used in your actual code.请检查您在实际代码中使用了正确的类型。 Here, it looks fine.在这里,看起来不错。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅使用localhost:8080 address运行servlet应用程序。这意味着我不想使用任何url模式 - how to run a servlet application with only localhost:8080 address.that means i dont want to any url patterns 我在哪里定义http:// localhost:8080 /“ homepage” - Where I define http://localhost:8080/“homepage” 当我通过tomcat启动我的应用程序时,为什么仍然得到“ http:// localhost:8080”? - Why I still get “http://localhost:8080” when I start my app by tomcat? Tomcat服务器上的Intellij Web应用程序显示http:// localhost:8080 / index.jsp而不是http:// localhost:8080 / myapp / index.jsp - Intellij web application on tomcat server shows http://localhost:8080/index.jsp instead of http://localhost:8080/myapp/index.jsp 我已经安装了Apache Tomcat 7,并且在浏览器中转到http:// localhost:8080时,看到以下错误:HTTP状态500 - I have installed Apache Tomcat 7 and when I goto http://localhost:8080 in my browser, I see the following error : HTTP Status 500 当Java HTTP Servlet会话结束时,如何运行任意代码? - How do I run arbitrary code when a Java HTTP Servlet session ends? java.io.FileNotFoundException: http://localhost:8080/manager/text/list - java.io.FileNotFoundException: http://localhost:8080/manager/text/list Tomcat在Eclipse运行时打不开http://localhost:8080/ - Cannot open http://localhost:8080/ when Tomcat is running in Eclipse 如何在 Jetty 的 http://localhost:8080/test/ 中去掉多余的 /? - How to I get rid of extra / in http://localhost:8080/test/ in Jetty? 我的 tomcat 正在运行,但无法连接到 http://localhost:8080 - My tomcat is running but I can't connect to http://localhost:8080
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM