在jsp文件中将Content-Type设置为application / json [英] Set Content-Type to application/json in jsp file

查看:795
本文介绍了在jsp文件中将Content-Type设置为application / json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些jsp文件,它作为响应返回一些json字符串。但是我看到Content-Type自动设置为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(睡觉为+ millis +millis);
Thread.sleep(millis);
%>
<%= retVal%>

我如何执行类似



<$ p的操作$ p> setHeader(Content-Type,application / json);

在这个例子中?

解决方案

您可以通过页面指令来完成。



例如:

 <%@ page language =java contentType =application / json; charset = UTF-8
pageEncoding =UTF-8%>




  • contentType =mimeType [; charset = characterSet]|
    text / html; charset = ISO-8859-1




MIME类型和JSP文件的字符编码用于它发送给客户端的
响应。您可以使用对JSP容器有效的任何MIME类型或
字符集。默认的MIME
类型是text / html,默认字符集是ISO-8859-1。



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?

解决方案

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.

这篇关于在jsp文件中将Content-Type设置为application / json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆