Introduction to the

The Servletrequest is a service function created by Tomcat that is passed to the servlet and contains information about the client. HttpServletRequest is based on the HTTP protocol

Servletrequest related functions

  1. Object getAttribute(String name); void setAttribute(String name, Object o); Void removeAttribute(String name) Returns a domain object. This field object is similar to the context, but is called for functions in the Requestdespatcher.
  2. Enumeration getAttributeNames ()
  3. String getCharacterEncoding() Header information, encoding mode.
  4. Void setCharacterEncoding(String env
  5. Int getContentLength() retrieves the length of the content, if the length position returns -1
  6. The same goes for long getContentLengthLong ()
  7. String getContentType() Specifies the type of the content
  8. ServletInputStream getInputStream() gets the input stream for the request
  9. Enumeration getParameterNames (); String getParameter (String name); String [] getParameterValues (String name); Map

    getParameterMap() gets query object information
    ,string[]>
  10. String getProtocol() Returns the protocol, for example, HTTP/1.1
  11. String getScheme() for example, Http
  12. String getServerName()
  13. Int getServerPort() specifies the start number
  14. BufferedReader getReader() opens the character input stream
  15. String getRemoteAddr()
  16. String getRemoteHost()
  17. Void removeAttribute (String name); Enumeration getLocales() returns the accept-language of the local message of the client if the local message is not called directly
  18. Boolean isSecure() Specifies whether to use a security protocol
  19. RequestDispatcher getRequestDispatcher(String Path) invokes the request handler
  20. ServletContext getServletContext() calls the environment
  21. AsyncContext startAsync (); AsyncContext startAsync(ServletRequest ServletRequest, ServletResponse ServletResponse) async mode
  22. Boolean isAsyncStarted() checks whether it is in asynchronous mode
  23. Boolean isAsyncSupported() Whether asynchrony is supported
  24. AsyncContext getAsyncContext() returns the entered async mode
  25. DispatcherType getDispatcherType() Is related to the dispatcher, which is related to the listener

Httpservletrequest functions

  1. String getAuthType() Authentication mode: There are three authentication modes: basic authentication, form authentication, client authentication, and other diagnostic authentication. If there is no authentication NULL. No study of certification, not in-depth.
  2. Cookie[] getCookies() : Returns cookies, there will be a topic for cookies. A cookie is a message sent by the server to the client for marking
  3. Long getDateHeader(String name) Returns a time object, such as if-modified-since.
  4. String getHeader(String name) Returns a character object whose header is name. Remember that name is case sensitive
  5. Enumeration getHeaders(String name) Returns objects with multiple values
  6. Enumeration getHeaderNames() returns all headers
  7. Int getIntHeader(String name) returns a positive header
  8. String getMethod() returns the method
  9. String getPathInfo() returns address information, which is used for directories with matching addresses, between the servlet address and query, starting with a slash

    <servlet>
        <servlet-name>xxxa</servlet-name>
        <servlet-class>AServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>xxxa</servlet-name>
        <url-pattern>/aservlet/*</url-pattern>
    </servlet-mapping>
    
http://localhost:8080/web11/aservlet/234234/4312

print234234/4312: /Copy the code
  1. Translated extra address information into real addresses. It doesn’t make any sense. For example, the above return D:\java_temp\plug1\out\artifacts\web11_war_exploded\234234\4312
  2. String getContextPath() returns the servlet address, or an empty String if it is the root address. There may be multiple getContextPath() objects that return different values from the context
  3. String getRemoteUser() Specifies the authentication name
  4. Boolean isUserInRole(String role) Authentication related
  5. Principal getUserPrincipal() Security level related to authentication
  6. String getRequestedSessionId() Returns the sessionID
  7. String getRequestURI() returns something between the protocol and query
  8. StringBuffer getRequestURL() returns the full URL without query information
  9. String getServletPath() returns the address of the servlet, starting with a slash
  10. HttpSession getSession(Boolean create) Returns a session, and if not, creates a new one based on the value of create
  11. HttpSession getSession()等同于HttpSession getSession(true)
  12. String changeSessionId() returns a new session
  13. Boolean isRequestedSessionIdValid () is related to the session
  14. isRequestedSessionIdFromCookie
  15. boolean isRequestedSessionIdFromURL()
  16. Boolean Authenticate (HttpServletResponse Response) authorization related
  17. Void logout (); void login(String username, String password)
  18. The Collection getParts (); Part getPart(String name) these two functions are mainly for post multipart/form-data processing.
  19. T upgrade(Class handlerClass) This is a protocol upgrade that returns a new object with a Class object. It doesn’t seem to help

Related classes RequestDispatcher

RequestDispatcher is a request resource scheduler that invokes related servlets and JSPS in the request. There are two functions:

  • Void Forward (ServletRequest Request,ServletResponse Response) Void Forward (ServletRequest Request,ServletResponse Response) Void Forward (ServletRequest Request,ServletResponse Response) Void Forward (ServletResponse Response) Void Forward (ServletRequest Request,ServletResponse Response
  • Void include(ServletRequest Request,ServletResponse Response) does not change the header or status code of the response.

URL encoding

For parameters in URL, application/ X-www-form-urlencoded method is adopted by default.

  • Requestcharsettpye is set to UTF-8 (null, ISO8895 is the default if the data is read directly), the data will be garbled. Therefore, the pre-set type is mandatory
  • Get command: Data is also url encoded and automatically processed by the browser and server. Parameter data is stored in the URL. However, the type data without GET does not have Application/X-www-form-urlencoded, and the encoding mode is UTF-8, so parameters can be read directly without setting. You don’t have to set this for a very strange reason. Probably utF-8, tomcat’s default setting
  • Urlencode.code () URLDecode. Decode ()
  • There is also the URI encoding, which differs from the URL encoding by space. The URI encoding space is %20 and the URL encoding is +