ServletRequest
object that represents the details of the HTTP request,
namely the HTTP method type, parameters, headers, and cookies.
This object encapsulates all the information sent by the client and provides methods to access this data programmatically.ServletRequest
object as a parameter to the service()
method of the HttpServlet
class.
This method automatically dispatches to the corresponding doHTTP_METHOD_NAME
method, which matches the HTTP method type of the request
(GET, POST, HEAD, OPTIONS, TRACE, PUT, and DELETE).
This dispatching mechanism allows servlets to handle different types of HTTP requests appropriately.doHTTP_METHOD_NAME
method in your servlet to write servlet-specific code.
However, the default implementations of the doOptions()
, doTrace()
, and doHead()
methods in the HttpServlet
class are usually sufficient to handle HTTP OPTIONS, TRACE, and HEAD requests.
For most web applications, you will primarily override doGet()
and doPost()
methods.ServletRequest
interface provides several convenient methods for accessing request parameters.
These methods handle the parsing and decoding of parameters automatically, making them the preferred approach for most scenarios.Enumeration<String> getParameterNames()
:String getParameter(String)
:<form name="myForm" method="GET" action="MyServlet1"> <input type="checkbox" name="myCheckbox1" value="0" /> <input type="checkbox" name="myCheckbox1" value="1" /> <input type="submit" name="mySubmit" value="Submit" /> </form>If both checkboxes are checked, the URL string will look like:
http://localhost:8080/mywebapp/MyServlet1?myCheckbox1=0&myCheckbox1=1&mySubmit=Submit
getParameter("myCheckbox1")
will return either "0" or "1", but not both.
In fact, the returned value corresponds to the first element of the array returned by String[] getParameterValues(String)
.String[] getParameterValues(String)
:getParameterValues("myCheckbox1")
will return the array ["0", "1"].Map<String, String[]> getParameterMap()
:Map
object (a hash table of key/value pairs), where:getParameterMap().put("myCheckbox1", new String[1])
will result in the following error:java.lang.IllegalStateException: No modifications are allowed to a locked ParameterMap org.apache.catalina.util.ParameterMap.put(ParameterMap.java:170)However, it is possible to modify the values within the array of a map entry, for example:
String[] values1 = (String[]) request.getParameterMap().get("myCheckbox1"); for (int i = 0; i < values1.length; i++) values1[i] = "New Value-" + i;Here is an example that displays the parameter names and values from the HTTP request (based on the form example above):
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.write("<html><head><title>test-getParameterMap</title></head><body>"); Set<String> keys = request.getParameterMap().keySet(); Iterator<String> keyIter = keys.iterator(); while (keyIter.hasNext()) { String key = (String) keyIter.next(); String[] values = (String[]) request.getParameterMap().get(key); out.write( " - key : " + key + " :<BR />" ); for (int i = 0; i < values.length; i++) { out.write( " " + (i+1) + ") value : " + values[i] + "<BR />" ); } out.write( "<BR /><BR />" ); } out.write("</body></html>"); out.close(); }Code execution result:
- key : myCheckbox1 : 1) value : 0 2) value : 1 - key : mySubmit : 1) value : Submit
java.io.BufferedReader getReader()
:ServletInputStream getInputStream()
:getInputStream()
first and then getReader()
, you will get the following error:
java.lang.IllegalStateException: getInputStream() has already been called for this request org.apache.catalina.connector.Request.getReader(Request.java:1212)And if you call
getReader()
first and then getInputStream()
, you will get the following error:
java.lang.IllegalStateException: getReader() has already been called for this request org.apache.catalina.connector.Request.getInputStream(Request.java:1058)Additionally, if you have already called any of the parameter methods (
getParameter()
, getParameterValues()
, or getParameterMap()
), the servlet container may have already consumed the request body, making these methods unavailable.<form name="myForm" method="POST" action="MyServlet1"> <input type="checkbox" name="myCheckbox1" value="0" /> <input type="checkbox" name="myCheckbox1" value="1" /> <input type="submit" name="mySubmit" value="Submit" /> </form>Servlet code:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.write("<html><head><title>test-getInputStream</title></head><body>"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(request.getInputStream())); String requestBodyLine; while ((requestBodyLine = bufferedReader.readLine()) != null) { StringTokenizer stringTokenizer = new StringTokenizer(requestBodyLine, "&"); while (stringTokenizer.hasMoreTokens()) { out.write(stringTokenizer.nextToken() + "<br />"); } } out.write("</body></html>"); out.close(); }Code execution result:
myCheckbox1=0 myCheckbox1=1 mySubmit=Submit
Enumeration<String> getHeaderNames()
:String getHeader(String)
:Enumeration<String> getHeaders(String)
:long getDateHeader(String)
:java.lang.IllegalArgumentException
if the header value cannot be converted to a valid date format.int getIntHeader(String)
:java.lang.NumberFormatException
if the header value cannot be converted to a valid integer.Cookie[] getCookies()
:Cookie
objects representing all cookies sent by the client with the HTTP request, or null if no cookies are present.Cookie
object encapsulates the cookie data and has:Cookie(String name, String value)
creates a new Cookie object with the specified name and value.getName()
, getValue()
, getDomain()
, getPath()
, and getMaxAge()
.void setAttribute(String, Object) void removeAttribute(String) Object getAttribute(String) Enumeration<String> getAttributeNames()
null
, the attribute will be removed from the request scope (same effect as calling removeAttribute
).ATTRIBUTE_NAME
.setAttribute
is called with a name that already exists,
the existing value will be replaced with the new one.