Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the use of "response.setContentType("text/html")" in servlet

Tags:

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>");       }     } 
like image 912
gowthaman Avatar asked Jan 12 '13 06:01

gowthaman


People also ask

Which method is used to specify that HTTP response would be an HTML content?

void setContentLength(int len) Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length header.

What does Response getWriter do?

response. getWriter() returns a PrintWriter object that can send character text to the client. Calling flush() on the PrintWriter commits the response.

What is getParameter in servlet?

getParameter. java.lang.String getParameter(java.lang.String name) Returns the value of a request parameter as a String , or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.


2 Answers

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.

like image 73
Damien_The_Unbeliever Avatar answered Sep 30 '22 11:09

Damien_The_Unbeliever


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.

like image 22
Aniket Kulkarni Avatar answered Sep 30 '22 11:09

Aniket Kulkarni