A web application is built using Servlet technology (resides at the server-side and generates a dynamic web page). Because of the Java programming language, servlet technology is dependable and scalable. CGI (Common Gateway Interface) scripting language was widely used as a server-side programming language prior to Servlet.
Servlet - Response
A servlet can use this object to help it provide a response to the client. A ServletResponse object is created by the servlet container and passed as an argument to the servlet's service function.
Use the ServletOutputStream supplied by getOutputStream to deliver binary data in a MIME body response (). Use the PrintWriter object given by getWriter to deliver character data (). Use a ServletOutputStream and manually control the character sections to blend binary and text data, for example, to generate a multipart response.
The setCharacterEncoding(java.lang.String) and setContentType(java.lang.String) methods can be used to provide the charset for the MIME body response, or the setLocale(java.util.Locale) method can be used to specify it implicitly. Implicit requirements are overridden by explicit specifications. ISO-8859-1 will be used if no charset is supplied. For the character encoding to be utilized, the setCharacterEncoding, setContentType, or setLocale methods must be called before getWriter and before committing the response.
Some Important Methods of ServletResponse
Methods
| Description
|
---|
String getCharacterEncoding() | It returns the name of the MIME charset that was used in the body of the client response. |
String getContentType() | It returns the response content type. e.g. text, HTML etc. |
 ServletOutputStream getOutputStream() | This method returns a ServletOutputStream that may be used to write binary data to the response. |
PrintWriter getWriter() | The PrintWriter object is used to transmit character text to the client. |
void setContentLength(int len) | Sets the length of the response's content body. This function sets the HTTP Content-Length header in HTTP servlets. |
void setContentType(String type) | Sets the type of the response data. |
void setBufferSize(int size) | specifies the recommended buffer size for the response's body. |
int getBufferSize() | Returns the buffer size |
void flushBuffer() | Any material in the buffer will be forced to be written to the client. |
boolean isCommitted() | If the response has been committed, this method returns a boolean. |
void setLocale(Locale loc) | If the answer hasn't been committed yet, it sets the response's location. |
void reset() | Clears the buffer's data, as well as the headers and status code. To acquire a comprehensive list of ways, go here. |
Implementation: The setContentType() and getWriter() methods of the ServletResponse interface were utilised in the example below.
A. File: index.html
HTML
<html>
<body>
<title> GEEKSFORGEEKS </title>
<form action="GFG" method="get">
Enter your username:
<br><br>
<input type="text" name="uname">
<br><br>
<input type="submit" value="login">
</form>
</body>
</html>
B. Application File (GFG.java)
Java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class GFG extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pwriter=res.getWriter();
String name=req.getParameter("uname");
pwriter.println("This is user details page:");
pwriter.println("Hello "+name);
pwriter.close();
}
}
C. web.xml
XML
<web-app>
<servlet>
<servlet-name>GFG</servlet-name>
<servlet-class>GFG</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GFG</servlet-name>
<url-pattern>/GFG</url-pattern>
</servlet-mapping>
</web-app>
Output:
First Screen shows the following output:
Second Screen shows the following output:
Similar Reads
JSP - Server Response In JavaServer Pages development, the server response refers to the data sent back to the client from the server after receiving and processing the client request, and the server response typically includes the dynamically generated by the HTML, XML, JSON, or other based on the JSP file logic and dat
6 min read
Servlet API Servlets are the Java programs that run on the Java-enabled web server or application server. They are used to handle the request obtained from the webserver, process the request, produce the response, then send a response back to the webserver. In Java, to create web applications we use Servlets. T
6 min read
Servlet - Cookies Cookies are the textual information that is stored in key-value pair format to the client's browser during multiple requests. It is one of the state management techniques in session tracking. Basically, the server treats every client request as a new one so to avoid this situation cookies are used.
4 min read
Servlet - Form A web page is the combination of many input elements such as label, text box, checkbox, options, images, etc., It can be prepared by enclosing all the input elements inside an "HTML FORM" on the server-side with java servlet. Usually, an HTML form collects data from the user via these input elements
7 min read
Servlet - RequestDispatcher The RequestDispatcher is an Interface that comes under package javax.servlet. Using this interface we get an object in servlet after receiving the request. Using the RequestDispatcher object we send a request to other resources which include (servlet, HTML file, or JSP file). A RequestDispatcher obj
5 min read