ServletResponse Interface |
public interface ServletResponse |
The ServletResponse interface declares methods that are used to assist the servlet in sending a response to the client machine. Example: Using ServletResponse This example uses the ServletResponse object that is automatically passed to the service() method to send an image file back to the client machine. A ServletContext object is used to assign the image file to an InputStream. The ServletResponse object is then used to set the content type of the response and return the ServletOutputStream object used to write the image.
                import javax.servlet.*;
         import java.io.*;
         public class ResponseServlet extends GenericServlet {
           int c;
           public void service(ServletRequest request, ServletResponse response)
                   throws ServletException, IOException, FileNotFoundException {
             // A ServletContext object is used to assign an image file to an
             // InputStream
             ServletContext sc = getServletContext();
             InputStream is = sc.getResourceAsStream("/gardening.jpg");
             // The ServletResponse object passed to the service() method is used
             // to set the content type of the response and to return the
             // ServletOutputStream object that is used to send the image file
             // back to the client machine.
         response.setContentType("image/jpg");
             ServletOutputStream sos = response.getOutputStream();
             while ((c = is.read()) != -1) {
               sos.write(c);
             }
             is.close();
             sos.close();
           }
         }

flushBuffer() |
public void flushBuffer() throws IOException | Method
flushBuffer() causes any content stored in the buffer to be written to the client. Calling this method will also commit the response, meaning that the status code and headers will be written.

getBufferSize() |
public int getBufferSize() | Method
getBufferSize() returns the buffer size used for the response, or 0 if no buffering is used.

setBufferSize() |
public void setBufferSize(int size) | Method
setBufferSize() requests a buffer size to be used for the response. The actual buffer size will be at least this large.

setContentLength() |
public void setContentLength(int length) | Method
setContentLength() sets the length of the body of the reponse.

setContentType() |
public void setContentType(String type) | Method
setContentType() sets the content type of the response sent to the server. The String argument specifies a MIME type and may also include the type of character encoding, for example "text/plain; charset=ISO-8859-1".

getCharacterEncoding() |
public String getCharacterEncoding() | Method
getCharacterEncoding() returns a String object containing the character encoding used in the body of the response. The default is "ISO-8859-1", which corresponds to Latin-1.

getOutputStream() |
public ServletOutputStream getOutputStream() throws IOException | Method
getOutputStream() returns a ServletOutputStream object that can be used to write the response as binary data.

getWriter() |
public PrintWriter getWriter() throws IOException | Method
getWriter() returns a PrintWriter object that can be used to write the response as character data.

isCommitted() |
public boolean isCommitted() | Method
isCommitted() returns true if the response has been committed, meaning that the status code and headers have been written.

getLocale() |
public Locale getLocale() | Method
getLocale() returns the locale that has been assigned to the response. By default, this will be the default locale for the server.

setLocale() |
public void setLocale(Locale locale) | Method
setLocale() specifies the locale that will be used for the response.

reset() |
public void reset() throws IllegalStateException | Method
reset() clears the status code and headers and any data that exists in the buffer. If the response has already been committed, calling this method will cause an exception to be thrown.