0

gif of file download pop up

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.

below is the screen shot of postman - postman returns a 404

postman on localhost 8080

below is the web.xml for mapping

<?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

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.
      }
}
Jon
  • 17
  • 1
  • 10
  • The postman image and your summary refer to two different URLs. Which is the right one? – nitind Jan 02 '17 at 00:52
  • @nitind the postman is for localhost 8080 and the Eclipse run on the servlet does not go to localhost 8080, it pops up a FILE DOWNLOAD, if you see the gif, you can see the reaction. – Jon Jan 02 '17 at 00:56
  • You can persuade IE to show JSON in the browser. See http://stackoverflow.com/a/2492211/. – Luke Woodward Jan 02 '17 at 10:50

5 Answers5

1

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. 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

That is it, (1) remove the setContentType AND (2) use the absolute URL WITH the Project name

http://localhost:8080/webData/queryreturn
Jon
  • 17
  • 1
  • 10
1

I was reading your question and the answers. I think that behaviour is only IE issue. Please checkout the same code opening Google Chrome, Mozilla, Opera or another. 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).

I hope you will got it. Regards!

Gonza
  • 163
  • 3
  • 11
  • thank you. HttpServlet is what I used, I am currently trying to decide to learn and use either Path (JAX-RS) or RequestMapping (Spring) . – Jon Jan 02 '17 at 11:14
  • Great Jon, I wish you success with Servlets. – Gonza Jan 02 '17 at 11:20
  • thank you, my remaining issue WHICH is from the beginning, how to load data in my grid - please if you have a moment, please view my next post at [link]http://stackoverflow.com/questions/41426096/paramquery-wont-load-servlet-url-json-string[link] – Jon Jan 02 '17 at 11:39
0

Internet Explorer handles "application/json" as a text file. 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?

Calvin
  • 53
  • 6
  • yes, you are correct. I just needed to remove the setContentType. I discovered this with a massive google search-a-thon from a website in Singapore. – Jon Jan 02 '17 at 09:51
  • Please keep the `application/json` content type. Other browsers like Firefox and Chrome will block the script from parsing it because of the mismatching content type (JSON returning as `text/html`). – Calvin Jan 02 '17 at 15:10
  • nope... it runs the same in Firefox and Chrome and Internet Explorer as well as in the Eclipse internal browser – Jon Jan 02 '17 at 15:14
  • You are correct that they return the same. For me I had to make sure the content type matches what I return because my sysadmin adds a `X-Content-Type-Options=nosniff` ([related question](https://stackoverflow.com/questions/18337630/what-is-x-content-type-options-nosniff)) security header as recommended by OWASP ([link](https://www.owasp.org/index.php/OWASP_Secure_Headers_Project#X-Content-Type-Options)). – Calvin Jan 02 '17 at 16:00
0

You can use Advanced Rest Client plugin in Chrome, Firebox and Safari https://github.com/wiztools/rest-client, https://insomnia.rest/ for Windows

GopalKrish
  • 25
  • 7
  • GetPostman works great, now that I have the absolute URL with the Project name. Thank you. – Jon Jan 02 '17 at 09:53
0

The reason for the error in postman is due to incorrect uri. Looks like you missed the application name. Please try this "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.

Ratha
  • 46
  • 5
  • yes you are correct, I found the solution from a massive google search-a-thon on a site in Singapore, thank you. – Jon Jan 02 '17 at 09:52