什么是“response.setContentType(" text / html")”"在servlet中 [英] what is the use of "response.setContentType("text/html")" in servlet

查看:88
本文介绍了什么是“response.setContentType(" text / html")”"在servlet中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class HelloWorld extends HttpServlet{ 
      public void doGet(HttpServletRequest request, 
      HttpServletResponse response)
      throws ServletException,IOException{
      **response.setContentType("text/html");**
      PrintWriter pw = response.getWriter();
      pw.println("<html>");
      pw.println("<head><title>Hello World</title></title>");
      pw.println("<body>");
      pw.println("<h1>Hello World</h1>");
      pw.println("</body></html>");
      }
    }


推荐答案

内容HTTP响应中包含类型,因为内容中字节序列值的相同字节可以多种方式解释。(*)

Content types are included in HTTP responses because the same, byte for byte sequence of values in the content could be interpreted in more than one way.(*)

请记住http可以传输不仅仅是HTML(js,css和图像都是明显的例子),在某些情况下,接收器将不知道它将接收什么类型的对象。

Remember that http can transport more than just HTML (js, css and images are obvious examples), and in some cases, the receiver will not know what type of object it's going to receive.

(*)这里显而易见的是XHTML - 这是XML。如果它的内容类型为 application / xml ,则接收方将其视为XML。如果它被提供为 application / xhtml + xml ,那么它应该被视为XHTML。

(*) the obvious one here is XHTML - which is XML. If it's served with a content type of application/xml, the receiver ought to just treat it as XML. If it's served up as application/xhtml+xml, then it ought to be treated as XHTML.

这篇关于什么是“response.setContentType(&quot; text / html&quot;)”&quot;在servlet中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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