Request And Response Interface
The ServletRequest Interface:
The ServletRequest interface contains various methods to handle client requests to access a servlet.The following describes various methods of the ServletRequest interface:
1. public String getParameter(String paramName)
Returns a String object that specifies the value of a particular request parameter.
2. public String[] getParameterValues(String paramName)
Returns an array of String objects that contains all the values of the request parameter.
3. public Enumeration getParameterNames()
Returns an Enumeration containing all the parameter names as String objects that a servlet request contains.
4. public String getRemoteHost()
Returns a String that specifies the full-qualified name of the computer from which the request is sent.
5. public Sring getRemoteAddr()
Returns a String that specifies the IP address of the computer from which the request is sent.
The following describes various methods of HttpservletRequest:
1. public String getHeader(String fieldname)
Returns the value of the request header field ,such as Cache-control and Accept - Langauge specified in parameter.
2. public Enumeration getHeaders(String sname)
Returns all the values associated with a specific request haeader as an Enumeration of String objects.
3. public Enumeration getHeaderNames()
The HttpServletResponse interface extends the ServletResponse interface and provides method to handle response, status codes and response headers for servlets that communicates using HTTP.
The ServletResponse Interface:
The ServletResponse interface contains various methods that enable a servlet to respond to the client requests.
A servlet can send the response either as character or binary data.
The PrintWriter stream can be used to send character data as servlet response and ServletOutputStream stream to send to binary data as servlet response.
The various methods of the ServletResponse interface are:
1. public ServletOutputStream getOutputstream() throws IOException
Returns an object of the ServletOutputStream class that represents an output stream to send binary data as response.
2. public PrintWriter getWriter() throws IOException
-Returns an object of the PrintWriter class that servlet uses to send character data as response.
3. public void setContentType(String type)
Sets the Multipurpose Internet Mail Extensions (MIME) type for the servlet response. Some of the MIME types are text/plain, image/jpeg, and text/html.
4. void setHeader(String hname, String hvalue)
Sets the value of header, hname to hvalue. If the header has already been set, the new value overwrites the existingvalue.
5. void setIntHeader(String hname, int hvalue)
Sets the value of the header, hname to the int value, hvalue.
If the header has already been set, the new value overwrites the existing value.
6. void setDateHeader(String hname, long datev)
Sets the value of the header, hname with a long value datev. The datev represents the number of milliseconds since the midnight of January 1, 1970, GMT.
7. void addHeader(String hname, String hvalue)
Adds a header, hname with value, hvalue.This method adds a new header if the header already exists.
8. void addIntHeader(String hname, int hvalue)
Adds a header, hname with an integer value, hvalue
9. void addDateHeader (String hname, long datev)
Adds a header named hname with value equal to given date, datev. The value of the datev must be in milliseconds, which starts since midnight, January 1, 1970, GMT.
10.boolean containsHeader(String hname)
Returns true if the header , hname has already been set with a specific value and false , if not.
11. void sendRediect(String url)
Redirects a request to the specified URL