在servlet中设置response.setContentType(“application/json”)返回json格式数据类型时在ie浏览器中提示下载文件。这种现象是ie浏览不支持application/json数据格式引起的,只要在servlet中再填上一句response.setHeader("Content-Type", "text/html")就能解决问题(应为浏览器对数据格式的识别靠的是响应头里面的Content-Type,所以,此种情况下页面在处理返回数据的时候,要先利用js把文本格式转化为json格式:eval()函数可以实现此功能)。
(注意:有文章说,ie浏览器重ie10开始就开始支持application/json了,但是我亲测过后并不是这个样子,不说ie10,就连ie11也不支持,也有可能是我的浏览器问题)
乍一看response.setContentType(XX)和response.setHeader("Content-Type", XX)不是一样的概念吗,其实并不是这样子的,前者设置返回的数据的类型,和浏览器没多大关系(其实也有关系,在设置前者而不设置后者的情况下,后者的值默认为前者,但是两者确实是有本质的区别的),后者设置的是相应头参数,响应头中的Content-Type决定了浏览器以什么方式来处理相应内容。
package com.sends.test; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TestServlet */ @WebServlet("/TestServlet") public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public TestServlet() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); //response.setHeader("Content-Type", "text/html"); PrintWriter writer = response.getWriter(); writer.write("{\"msg\":\"success\"}"); writer.flush(); writer.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.sends.test; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TestServlet */ @WebServlet("/TestServlet") public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public TestServlet() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-Type", "text/html"); PrintWriter writer = response.getWriter(); writer.write("{\"msg\":\"success\"}"); writer.flush(); writer.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.sends.test; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TestServlet */ @WebServlet("/TestServlet") public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public TestServlet() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-Type", "application/json"); PrintWriter writer = response.getWriter(); writer.write("{\"msg\":\"success\"}"); writer.flush(); writer.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }