Home Java Javaee6 ServletResponse methods Servlet 3.0



ServletResponse methods Servlet 3.0
Posted on: December 21, 2011 at 12:00 AM
In this tutorial you will learn about the method of ServletResponse.

ServletResponse methods - Servlet 3.0

In this tutorial you will learn about the method of ServletResponse.

ServletResponse interface method generally provides setter and getter methods to set and get the appropriate value.

Here I am giving commonly used methods of ServletResponse interface. These are as follows :

  • setContentType() : This method is used to set the content type in which you want to response request made by client.
  • getContentType() : This method is used when you want to check the content type is using to response.
  • getWriter() : This method gives an object of PrintWriter class through which the text is send to the client.
  • getLocale() : This method is used to get the Locale for the response.
  • setLocale() : This method is used for set the Locale of java.util.Locale for the response.

Example :

Here I am giving a simple example that will demonstrate you about some of these methods.

package roseindia.urlExample;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ServletResponseMethod")
public class ServletResponseMethod extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out= response.getWriter();
response.setContentType("text/html");
String cnt= response.getContentType();
out.println("Content Type = "+cnt);
int bs= response.getBufferSize();
out.println("<br>BufferSize= "+bs);
String chen= response.getCharacterEncoding();
out.println("<br>Charset = "+chen);
boolean bol= response.isCommitted();
out.println("<br>Is committed = "+bol);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}

Output :

When you will execute the above example you will get the output as :

Download Source Code

Related Tags for ServletResponse methods Servlet 3.0:


More Tutorials from this section

Ask Questions?    Discuss: ServletResponse methods Servlet 3.0  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.