Set Content-Type to application/json in jsp file(在 jsp 文件中将 Content-Type 设置为 application/json)
问题描述
我创建了一些 jsp 文件,它作为响应返回一些 json 字符串.但是我看到 Content-Type 自动设置为 txt
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
我的jsp代码看起来像
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");
在这个例子中?
推荐答案
你可以通过页面指令.
例如:
<%@ page language="java" contentType="application/json; charset=UTF-8"
pageEncoding="UTF-8"%>
- contentType="mimeType [ ;charset=characterSet ]" |"text/html;charset=ISO-8859-1"
JSP 文件使用的 MIME 类型和字符编码它发送给客户端的响应.您可以使用任何 MIME 类型或对 JSP 容器有效的字符集.默认 MIME类型为 text/html,默认字符集为 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 jsp 文件中将 Content-Type 设置为 application/json


基础教程推荐
- 多个组件的复杂布局 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 大摇大摆的枚举 2022-01-01