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 :
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 :

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.
Ask Questions? Discuss: ServletResponse methods Servlet 3.0
Post your Comment