Reputation: 1003
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>");
}
}
Upvotes: 22
Views: 207397
Reputation: 422
response.setContentType("text/html");
Above code would be include in "HTTP response" to inform the browser about the format of the response, so that the browser can interpret it.
Upvotes: 2
Reputation: 91
You have to tell the browser what you are sending back so that the browser can take appropriate action like launching a PDF viewer if its a PDF that is being received or launching a video player to play video file ,rendering the HTML if the content type is simple html response, save the bytes of the response as a downloaded file, etc.
some common MIME types are text/html,application/pdf,video/quicktime,application/java,image/jpeg,application/jar etc
In your case since you are sending HTML response to client you will have to set the content type as text/html
Upvotes: 3
Reputation: 12993
From JavaEE docs ServletResponse#setContentType
Sets the content type of the response being sent to the client, if the response has not been committed yet.
The given content type may include a character encoding specification, for example,
response.setContentType("text/html;charset=UTF-8");
The response's character encoding is only set from the given content type if this method is called before getWriter
is called.
This method may be called repeatedly to change content type and character encoding.
This method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after getWriter
has been called or after the response has been committed.
Containers must communicate the content type and the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the Content-Type header is used.
Upvotes: 14
Reputation: 18133
It is one of the MIME
type, in this case you are reponse header MIME
type to text/html
it means it displays html type. It is a information to browser. There are other types you can set to display excel, zip etc. Please see MIME Type
for more information
Upvotes: 1
Reputation: 483
It means what type of response you want to send to client, some content types like :
res.setContentType("image/gif");
res.setContentType("application/pdf");
res.setContentType("application/zip");
Upvotes: 6
Reputation: 239814
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.(*)
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.
(*) 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.
Upvotes: 22