English 中文(简体)
Set content-Type to application/json in jsp file
原标题:Set Content-Type to application/json in jsp file

我编造了一些小篇档案,作为回应,一些json 扼杀。 但是,我看到,内容提式包裹会自动装到txt。

我的jsp法典像我一样。

<%@ 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%>

我如何做这样的事情

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

这一例子?

最佳回答

http://java.sun.com/products/jsp/tags/11/syntaxref11.fm7.html。 页: 1

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.

问题回答

对于这部法典,它也应该同样发挥作用。

<%
    //response.setContentType("Content-Type", "application/json"); // this will fail compilation
    response.setContentType("application/json"); //fixed
%>

@Petr Mensik &kensen john

由于我不得不根据某些URL参数确定不同的内容类型,因此我无法使用该页指令。 我将在此重申我的准则,因为它与日本人有着相当共同之处:

    <%
        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) {
            // IMPORTANT! Sanitise the "callback",
            // do not use user input directly in the response!
            // This is vulnerable to XSS attacks.
            output += callback + "(";
        }
    
        output += jsonObj.toString();
    
        if (callback != null) {
            output += ");";
        }
    %>
    <%=output %>

提供回响时,返回:

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

内容类型“文字/文字”

当NOT供应时,返回:

    {...JSON stuff...}

内容类型“应用/json”

[EDIT] I m 审查我的旧职位。 如果你利用这一解决办法,请你提供意见! 将用户投入直接用于应对措施,是危险的。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签