The ServletResponse represents the response which is sent back to the user. The servlet container creates a ServletResponse object and passes it as an argument to the servlet’s service method.
Important Methods of ServletResponse Interface
- public PrintWriter getWriter() : This method returns a PrintWriter object that can send character text to the client.
- public ServletOutputStream getOutputStream() : This method returns a ServletOutputStream suitable for writing binary data in the response.
- public String getCharacterEncoding() : This method returns the name of the MIME charset used.
- public void setContentType(String type) : Sets the content type of the response being sent as response.
- public int getBufferSize() : This method returns the actual buffer size used for the response. If no buffering is used, this method returns 0.
- public void flushBuffer() : Forces any content in the buffer to be written to the client.
- public void setLocale(Locale loc) : This method sets the locale of the response, if the response has not been committed yet.
- public boolean isCommitted(): This method returns a boolean indicating if the response has been committed.
- public void reset() : Clears the data of the buffer along with the headers and status code.
ServletResponseInterfaceExample.java
package com.javainterviewpoint; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class ServletResponseInterfaceExample implements Servlet { public void init(ServletConfig config) throws ServletException { } public void destroy() { } public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { //Getting the PrintWriter object PrintWriter out = response.getWriter(); //Setting the Content type response.setContentType("text/html"); out.println("Content Type : "+request.getContentType()); //Posting the response back String name = request.getParameter("name"); out.println("<br>Welcome \""+name+"\""); } public String getServletInfo() { return null; } public ServletConfig getServletConfig() { return null; } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>ServletsTutorial</display-name> <servlet> <servlet-name>ServletResponseInterfaceExample</servlet-name> <servlet-class>com.javainterviewpoint.ServletResponseInterfaceExample</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletResponseInterfaceExample</servlet-name> <url-pattern>/ServletResponseInterfaceExample</url-pattern> </servlet-mapping> </web-app>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>RequestInterfaceExample</title> </head> <body> <form action="./ServletResponseInterfaceExample" method = "POST"> Name : <input type="text" name="name"><br> <input type="submit" value="Submit Form"> <input type="reset" value="Reset Form"> </form> </body> </html>
Output
Hit on the Url : http://localhost:8080/ServletTutorial/index.jsp
Hit on the Submit button
Content Type : application/x-www-form-urlencoded
Welcome “JavaInterviewPoint”
Leave a Reply