Monday 16 April 2018

Introduction to HttpServletResponse

Introduction to HttpServletResponse, what is HttpServletResponse,HttpServletResponse,How to write characters in httpresponse,PrintWriter ,HttpServletResponse ,getWriter(),How to write bytes in http response,OutputStream getOutputStream(),HttpServletResponse,How to add headers in http response,addHeader(..), addDateHeader(..), addIntHeader(..),containsHeader(..),How to redirect http request to any other webpage on any webserver,sendRedirect(String url),How to set content type and content length headers,setCharacterEncoding(..), setContentLength(..),etContentType(..),how to set http status code for http response sent to http client,setStatus(int), sendError(int),sendError(int, String),

An instance of HttpServletResponse object is passed by the servlet container to the servlet and the servlet stuffs the httpresponse that it wants to send to the client (headers, body, cookies etc) in this object. 

When the servlet is done constructing the response, the container sends the response to the client in http protocol semantics.

How to write characters in httpresponse

To send response to the http client, you need a reference to the output stream of the httpresponse. This is provided by the PrintWriter object obtained from the method PrintWriter getWriter() of the HttpServletResponse object as shown below and then call the println(String) method on it.
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  
{  
    PrintWriter pWriter = resp.getWriter();  
    pWriter.println("It works.");  
}  

How to write bytes in http response

If you need to send bytes, instead of characters as http response, you need to get the Outputstream object using the method OutputStream getOutputStream() of the HttpServletResponse object. Then call the write(Bytes[]) method on it to send the bytes as shown below. 
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  
{  
    OutputStream out = resp.getOutputStream();  
    out.write("It works".getBytes());  
}  

How to add headers in http response

To set headers in the http response and check if specific headers are present, use the methods addHeader(..), addDateHeader(..)addIntHeader(..) and containsHeader(..) of the HttpServletResponse object. 
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  
{  
    resp.addHeader("lang""english");  
    resp.addDateHeader("createTime", Calendar.getInstance().getTimeInMillis());  
    resp.addIntHeader("iteration"8);  
    resp.containsHeader("lang");  

How to redirect http request to any other webpage on any webserver

To redirect an httprequest to any other url, you need to use the method sendRedirect(String url) of the HttpServletResponse object where target url is the location where you want to redirect the request. 
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  
{  
    resp.sendRedirect("/main/login.jsp");  

How to set content type and content length headers

To set the content encoding, content type and content length of the http response, you need to use the methods setCharacterEncoding(..)setContentLength(..) and setContentType(..) of the HttpServletResponse object.

These methods are essentially convenience methods for setting the corresponding headers Content-EncodingContent-Length & Content-Type.
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  
{  
    resp.setCharacterEncoding("utf-8");  
    resp.setContentLength(230);  
    resp.setContentType("text/html");  
}

how to set http status code for http response sent to http client

To set the response http status codes, you can use the methods setStatus(int)sendError(int) or sendError(int, String) of the HttpServletResponse object as shown below. 
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  
{  
    resp.setStatus(601);  
    //resp.sendError(602);  
    //resp.sendError(603, "Unauthorized access");  
}  







DashZin

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

1 comments: