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 :