ServletRequest Interface | |
public interface ServletRequest | |
The ServletRequest interface declares methods that are used to provide client request information to a servlet. The information can include parameter name-value pairs, attributes, and an input stream. A ServletRequest object is passed to the service() method defined in the Servlet interface as well as the forward() and include() methods from the RequestDispatcher interface. Example: Using ServletRequestThis example uses the ServletRequest object that is automatically passed to the service() method to access information about the request and about the client machine that made the request.
import javax.servlet.*;
import java.io.*;
import java.util.*;
public class RequestServlet extends GenericServlet {
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
PrintWriter pw = response.getWriter();
// The ServletRequest object passed to the service() method is used
// to obtain information about the request and about the client machine.
pw.println("IP address of the client: "
+ request.getRemoteAddr());
pw.println("Name of the client: " + request.getRemoteHost());
pw.println("Character encoding: "
+ request.getCharacterEncoding());
pw.println("Length of request: " + request.getContentLength());
pw.println("Type of request: " + request.getContentType());
pw.println("Request Protocol: " + request.getProtocol());
pw.println("Request scheme: " + request.getScheme());
pw.close();
}
}
|
getInputStream() | |
public ServletInputStream getInputStream() throws IOException | Method |
getInputStream() returns a ServletInputStream object that can be used to read the body of the request as binary data. |
getReader() | |
public BufferedReader getReader() throws IOException | Method |
getReader() returns a BufferedReader object that can be used to read the body of the request as character data. |
getLocale() | |
public Locale getLocale() | Method |
getLocale() returns the preferred locale of the client that made the request. |
getLocales() | |
public EnumerationgetLocales() | Method |
getLocales() returns an Enumeration containing, in descending order of preference, the locales that are acceptable to the client machine. |
getParameter() | |
public String getParameter(String name) | Method |
Parameters are name-value pairs that can be used to provide request-specific information to a servlet. For instance, they can identify which file the servlet should access. getParameter() returns a String object containing the value of the specfied parameter, or null if the parameter does not exist. |
getParameterNames() | |
public Enumeration getParameterNames() | Method |
Parameters are name-value pairs that can be used to provide request-specific information to a servlet. For instance, they can identify which file the servlet should access. getParameterNames() returns a Enumeration containing the parameters contained within the invoking ServletRequest object. |
getParameterValues() | |
public String[] getParameterValues(String name) | Method |
Parameters are name-value pairs that can be used to provide request-specific information to a servlet. For instance, they can identify which file the servlet should access. getParamterValues() is used when a parameter may have more than one value associated with it. The method returns a String array containing the values of the specfied parameter, or null if the parameter does not exist. |
getAttribute() | |
public Object getAttribute(String name) | Method |
getAttribute() returns the value of the specified request attribute name. The return value is an Object or sub-class if the attribute is available to the invoking ServletRequest object, or null if the attribute is not available. |
getAttributeNames() | |
public Enumeration getAttributeNames() | Method |
getAttributeNames() returns an Enumeration containing the attribute names available to the invoking ServletRequest object. |
removeAttribute() | |
public void removeAttribute(String name) | Method |
removeAttribute() makes the specified attribute unavailable to the invoking ServletRequest object. Subsequent calls to the getAttribute() method for this attribute will return null. |
setAttribute() | |
public void setAttribute(String name, Object value) | Method |
setAttribute() binds a value to a specified attribute name. Note that attributes will be re-set after the request is handled. |
getRequestDispatcher() | |
public RequestDispatcher getRequestDispatcher(String path) | Method |
A RequestDispatcher object sends requests from client machines to the appropriate resource (servlet, HTML file, etc.) on the server. The server creates the RequestDispatcher object which is used as a wrapper around a particular resource. getRequestDispatcher() returns an RequestDispatcher object that acts as a wrapper around the resource located at the specified path. The path must begin with "/" and can be a relative path. |
getRemoteAddr() | |
public String getRemoteAddr() | Method |
getRemoteAddr() returns a String object containing the IP address of the client machine that made the request. |
getRemoteHost() | |
public String getRemoteHost() | Method |
getRemoteHost() returns a String object containing the name of the client machine or the IP address if the name cannot be determined. |
getCharacterEncoding() | |
public String getCharacterEncoding() | Method |
getCharacterEncoding() returns a String object containing the character encoding used in the body of the request, or null if there is no encoding. |
getContentLength() | |
public int getContentLength() | Method |
getContentLength() returns the length of the body of the request in bytes, or –1 if the length is not known. |
getContentType() | |
public String getContentType() | Method |
getContentType() returns a String object containing the MIME type ("text/plain", "text/html", "image/gif", etc.) of the body of the request, or null if the type is not known. |
getProtocol() | |
public String getProtocol() | Method |
getProtocol() returns the name and version of the protocol used by the request. A typical return String would be "HTTP/1.1". |
getScheme() | |
public String getScheme() | Method |
getScheme() returns the scheme ("http", "https", "ftp", etc.) used to make the request. |
isSecure() | |
public boolean isSecure() | Method |
isSecure() returns true if the request was made using a secure channel, for example HTTPS. |
getServerName() | |
public String getServerName() | Method |
getServerName() returns a String object containing the name of the server that received the request. |
getServerPort() | |
public int getServerPort() | Method |
getServerPort() returns the port number that received the request. |