javax.servlet.http
Interface HttpServletRequest

All Superinterfaces:
ServletRequest
All Known Subinterfaces:
CauchoRequest
All Known Implementing Classes:
AbstractCauchoRequest, AsyncRequest, AsyncRequest, CauchoRequestWrapper, DispatchRequest, ErrorRequest, FastCgiProxy.ProxyRequest, ForwardRequest, HttpProxy.ProxyRequest, HttpServletRequestImpl, HttpServletRequestWrapper, IncludeRequest, LoadBalance.LoadBalanceRequest, LoginRequest, ProxyCacheRequest, ProxyRule.ProxyRequest, RequestAdapter, SetRequestSecureFilterChain.SecureServletRequestWrapper, StubServletRequest, StubSessionContextRequest

public interface HttpServletRequest
extends ServletRequest

HttpServletRequest encapsulates client request data.

URI

The servlet engine splits the URI into three useful sections: the context path (application prefix), the servlet path, and the path info.

For example, given an application prefix of '/myapp', the api /myapp/dir/test.jsp/data will be split as follows:

/myapp/dir/test.jsp/datagetRequestURI()
/myappgetContextPath()
/dir/test.jspgetServletPath()
/datagetPathInfo()

Useful headers

User-AgentString describing the browser's version

CGI equivalents

AUTH_TYPEgetAuthType()
CONTENT_TYPEgetContentType()
CONTENT_LENGTHgetContentLength()
PATH_INFOgetPathInfo()
PATH_TRANSLATEDgetPathTranslated()
QUERY_STRINGgetQueryString()
REMOTE_ADDRgetRemoteAddr()
REMOTE_HOSTgetRemoteHost()
REMOTE_USERgetRemoteUser()
REQUEST_METHODgetMethod()
SCRIPT_NAMEgetServletPath()
SERVER_NAMEgetServerName()
SERVER_PROTOCOLgetProtocol()
SERVER_PORTgetServerPort()

Form data

For form data, see ServletRequest.getParameter()


Field Summary
static java.lang.String BASIC_AUTH
          String identifier for basic authentication.
static java.lang.String CLIENT_CERT_AUTH
          String identifier for client cert authentication.
static java.lang.String DIGEST_AUTH
          String identifier for digest authenciation.
static java.lang.String FORM_AUTH
          String identifier for form authenciation.
 
Method Summary
 boolean authenticate(HttpServletResponse response)
           
 java.lang.String getAuthType()
          Returns the auth type, i.e.
 java.lang.String getContextPath()
          Returns the part of the URI corresponding to the application's prefix.
 Cookie[] getCookies()
          Returns an array of all cookies sent by the client.
 long getDateHeader(java.lang.String name)
          Converts a date header to milliseconds since the epoch.
 java.lang.String getHeader(java.lang.String name)
          Returns the first value for a request header.
 java.util.Enumeration<java.lang.String> getHeaderNames()
          Returns an enumeration of all headers sent by the client.
 java.util.Enumeration<java.lang.String> getHeaders(java.lang.String name)
          Returns all the values for a request header.
 int getIntHeader(java.lang.String name)
          Converts a header value to an integer.
 java.lang.String getMethod()
          Returns the HTTP method, e.g.
 Part getPart(java.lang.String name)
           
 java.util.Collection<Part> getParts()
           
 java.lang.String getPathInfo()
          Returns the URI part after the selected servlet and null if there is no suffix.
 java.lang.String getPathTranslated()
          Returns the physical path name for the path info.
 java.lang.String getQueryString()
          Returns the request's query string.
 java.lang.String getRemoteUser()
          Returns the remote user if authenticated.
 java.lang.String getRequestedSessionId()
          Returns the session id.
 java.lang.String getRequestURI()
          Returns the entire request URI
 java.lang.StringBuffer getRequestURL()
          Reconstructs the URL the client used for the request.
 java.lang.String getServletPath()
          Returns the URI part corresponding to the selected servlet.
 HttpSession getSession()
          Returns the current session, creating one if necessary.
 HttpSession getSession(boolean create)
          Returns a session.
 java.security.Principal getUserPrincipal()
          Returns the equivalent principal object for the authenticated user.
 boolean isRequestedSessionIdFromCookie()
          Returns true if the session came from a cookie.
 boolean isRequestedSessionIdFromUrl()
          Deprecated.  
 boolean isRequestedSessionIdFromURL()
          Returns true if the session came URL-encoding.
 boolean isRequestedSessionIdValid()
          Returns true if the session is valid.
 boolean isUserInRole(java.lang.String role)
          Returns true if the user is in the given role.
 void login(java.lang.String username, java.lang.String password)
           
 void logout()
           
 
Methods inherited from interface javax.servlet.ServletRequest
getAsyncContext, getAttribute, getAttributeNames, getCharacterEncoding, getContentLength, getContentType, getDispatcherType, getInputStream, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getParameter, getParameterMap, getParameterNames, getParameterValues, getProtocol, getReader, getRealPath, getRemoteAddr, getRemoteHost, getRemotePort, getRequestDispatcher, getScheme, getServerName, getServerPort, getServletContext, isAsyncStarted, isAsyncSupported, isSecure, removeAttribute, setAttribute, setCharacterEncoding, startAsync, startAsync
 

Field Detail

BASIC_AUTH

static final java.lang.String BASIC_AUTH
String identifier for basic authentication. Value "BASIC".

See Also:
Constant Field Values

CLIENT_CERT_AUTH

static final java.lang.String CLIENT_CERT_AUTH
String identifier for client cert authentication. Value "CLIENT_CERT".

See Also:
Constant Field Values

DIGEST_AUTH

static final java.lang.String DIGEST_AUTH
String identifier for digest authenciation. Value "DIGEST".

See Also:
Constant Field Values

FORM_AUTH

static final java.lang.String FORM_AUTH
String identifier for form authenciation. Value "FORM".

See Also:
Constant Field Values
Method Detail

getMethod

java.lang.String getMethod()
Returns the HTTP method, e.g. "GET" or "POST"

Equivalent to CGI's REQUEST_METHOD


getRequestURI

java.lang.String getRequestURI()
Returns the entire request URI


getRequestURL

java.lang.StringBuffer getRequestURL()
Reconstructs the URL the client used for the request.

Since:
Servlet 2.3

getContextPath

java.lang.String getContextPath()
Returns the part of the URI corresponding to the application's prefix. The first part of the URI selects applications (ServletContexts).

getContextPath() is /myapp for the uri /myapp/servlet/Hello,


getServletPath

java.lang.String getServletPath()
Returns the URI part corresponding to the selected servlet. The URI is relative to the application. Returns an emtpy string for a servlet matched on /*

Corresponds to CGI's SCRIPT_NAME getServletPath() is /servlet/Hello for the uri /myapp/servlet/Hello/foo. getServletPath() is /dir/hello.jsp for the uri /myapp/dir/hello.jsp/foo,


getPathInfo

java.lang.String getPathInfo()
Returns the URI part after the selected servlet and null if there is no suffix.

Corresponds to CGI's PATH_INFO

getPathInfo() is /foo for the uri /myapp/servlet/Hello/foo. getPathInfo() is /hello.jsp for for the uri /myapp/dir/hello.jsp/foo.


getPathTranslated

java.lang.String getPathTranslated()
Returns the physical path name for the path info.

Corresponds to CGI's PATH_TRANSLATED

Returns:
null if there is no path info.

getQueryString

java.lang.String getQueryString()
Returns the request's query string. Form based servlets will use ServletRequest.getParameter() to decode the form values.

Corresponds to CGI's PATH_TRANSLATED


getHeader

java.lang.String getHeader(java.lang.String name)
Returns the first value for a request header.

Corresponds to CGI's HTTP_*

 String userAgent = request.getHeader("User-Agent");
 

Parameters:
name - the header name
Returns:
the header value

getHeaders

java.util.Enumeration<java.lang.String> getHeaders(java.lang.String name)
Returns all the values for a request header. In some rare cases, like cookies, browsers may return multiple headers.

Parameters:
name - the header name
Returns:
an enumeration of the header values.

getHeaderNames

java.util.Enumeration<java.lang.String> getHeaderNames()
Returns an enumeration of all headers sent by the client.


getIntHeader

int getIntHeader(java.lang.String name)
Converts a header value to an integer.

Parameters:
name - the header name
Returns:
the header value converted to an integer

getDateHeader

long getDateHeader(java.lang.String name)
Converts a date header to milliseconds since the epoch.

 long mod = request.getDateHeader("If-Modified-Since");
 

Parameters:
name - the header name
Returns:
the header value converted to an date

getCookies

Cookie[] getCookies()
Returns an array of all cookies sent by the client.


getSession

HttpSession getSession(boolean create)
Returns a session. If no session exists and create is true, then create a new session, otherwise return null.

Parameters:
create - If true, then create a new session if none exists.

getSession

HttpSession getSession()
Returns the current session, creating one if necessary. Sessions are a convenience for keeping user state across requests.


getRequestedSessionId

java.lang.String getRequestedSessionId()
Returns the session id. Sessions are a convenience for keeping user state across requests.

The session id is the value of the JSESSION cookie.


isRequestedSessionIdValid

boolean isRequestedSessionIdValid()
Returns true if the session is valid.


isRequestedSessionIdFromCookie

boolean isRequestedSessionIdFromCookie()
Returns true if the session came from a cookie.


isRequestedSessionIdFromURL

boolean isRequestedSessionIdFromURL()
Returns true if the session came URL-encoding.


getAuthType

java.lang.String getAuthType()
Returns the auth type, i.e. BASIC, CLIENT-CERT, DIGEST, or FORM.


getRemoteUser

java.lang.String getRemoteUser()
Returns the remote user if authenticated.


isUserInRole

boolean isUserInRole(java.lang.String role)
Returns true if the user is in the given role.


getUserPrincipal

java.security.Principal getUserPrincipal()
Returns the equivalent principal object for the authenticated user.


isRequestedSessionIdFromUrl

boolean isRequestedSessionIdFromUrl()
Deprecated. 


authenticate

boolean authenticate(HttpServletResponse response)
                     throws java.io.IOException,
                            ServletException
Throws:
java.io.IOException
ServletException
Since:
Servlet 3.0

login

void login(java.lang.String username,
           java.lang.String password)
           throws ServletException
Throws:
ServletException
Since:
Servlet 3.0

logout

void logout()
            throws ServletException
Throws:
ServletException
Since:
Servlet 3.0

getParts

java.util.Collection<Part> getParts()
                                    throws java.io.IOException,
                                           ServletException
Throws:
java.io.IOException
ServletException
Since:
Servlet 3.0

getPart

Part getPart(java.lang.String name)
             throws java.io.IOException,
                    ServletException
Throws:
java.io.IOException
ServletException
Since:
Servlet 3.0