39

I am created some jsp file that returns as a response some json string. But I see that the Content-Type is set to txt automatically

My jsp code looks like

<%@ page import="java.util.Random" %>
<%@ page language="java" %>
<%@ page session="false" %>

<%
  String retVal = "// some json string";

     int millis = new Random().nextInt(1000);
     //    System.out.println("sleeping for " + millis + " millis");
     Thread.sleep(millis);
%>
<%=retVal%>

How can I perform something like

setHeader("Content-Type", "application/json");

in this example?

Hardik Mishra
  • 14,171
  • 9
  • 58
  • 94
Julias
  • 5,372
  • 14
  • 53
  • 78

3 Answers3

69

You can do via Page directive.

For example:

<%@ page language="java" contentType="application/json; charset=UTF-8"
    pageEncoding="UTF-8"%>
  • contentType="mimeType [ ;charset=characterSet ]" | "text/html;charset=ISO-8859-1"

The MIME type and character encoding the JSP file uses for the response it sends to the client. You can use any MIME type or character set that are valid for the JSP container. The default MIME type is text/html, and the default character set is ISO-8859-1.

Pau Kiat Wee
  • 9,135
  • 40
  • 38
12

Try this piece of code, it should work too

<%
    //response.setContentType("Content-Type", "application/json"); // this will fail compilation
    response.setContentType("application/json"); //fixed
%>
kensen john
  • 4,922
  • 3
  • 26
  • 35
Petr Mensik
  • 24,455
  • 13
  • 84
  • 111
  • but this throws exception like java.lang.IllegalStateException: getOutputStream() has already been called for this response – Karan Jun 05 '15 at 18:11
  • make sure you do *not output any single character* to the client before calling `setContentType(..)` (otherwise the content type is already fixed before that). Especially pay attention to spaces or newlines after %>, e.g. write move the closing %> of the first line to the second line and start immediately with the next – Andre Holzner Mar 21 '17 at 13:56
3

@Petr Mensik & kensen john

Thanks, I could not used the page directive because I have to set a different content type according to some URL parameter. I will paste my code here since it's something quite common with JSON:

    <%
        String callback = request.getParameter("callback");
        response.setCharacterEncoding("UTF-8");
        if (callback != null) {
            // Equivalent to: <@page contentType="text/javascript" pageEncoding="UTF-8">
            response.setContentType("text/javascript");
        } else {
            // Equivalent to: <@page contentType="application/json" pageEncoding="UTF-8">
            response.setContentType("application/json");
        }

        [...]

        String output = "";

        if (callback != null) {
            output += callback + "(";
        }

        output += jsonObj.toString();

        if (callback != null) {
            output += ");";
        }
    %>
    <%=output %>

When callback is supplied, returns:

    callback({...JSON stuff...});

with content-type "text/javascript"

When callback is NOT supplied, returns:

    {...JSON stuff...}

with content-type "application/json"

Gael
  • 251
  • 1
  • 5