Servlet-HttpServletRequest对象

概述

servlet-api 4.0.1版本

HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。

常用方法

HttpServletRequest接口

扩展ServletRequest接口以提供HTTP Servlet的请求信息。Servlet容器创建一个HttpServletRequest对象,并将其作为参数传递给Servlet的服务方法(doGet,doPost等)。

public String getAuthType(); // 返回用于保护servlet的认证方案的名称

public Cookie[] getCookies(); // 返回一个数组,其中包含客户端与此请求一起发送的所有Cookie对象

public String getContextPath(); // 返回请求URI中指示请求上下文的部分
// /xxx

public HttpSession getSession(); // 返回与此请求关联的当前HttpSession,get后创建session?

public long getDateHeader(String name); // 返回指定请求标头的值,作为代表Date对象的长值

public String getHeader(String name); // 以字符串形式返回指定请求标头的值。 不包含则返回null,多个返回第一个

public Enumeration<String> getHeaders(String name); // 以String对象的枚举形式返回指定请求标头的所有值

public Enumeration<String> getHeaderNames(); // 返回此请求包含的所有标头名称的枚举

public int getIntHeader(String name); // 以int形式返回指定请求标头的值。 如果请求中没有指定名称的标头,则此方法返回-1

default public HttpServletMapping getHttpServletMapping() {...} // 返回调用该HttpServletRequest的HttpServlet的HttpServletMapping

public String getMethod(); // 返回发出此请求的HTTP方法的名称,例如GET,POST或PUT

public String getPathInfo(); // 返回与客户端发出此请求时发送的URL相关的任何其他路径信息

public String getPathTranslated(); // 返回servlet名称之后但查询字符串之前的所有其他路径信息,并将其转换为真实路径

public String getQueryString(); // 返回路径后面的请求URL中包含的查询字符串

public String getRemoteUser(); // 如果用户已通过身份验证,则返回发出此请求的用户的登录名;如果用户未通过身份验证,则返回null

public boolean isUserInRole(String role); // 返回一个布尔值,指示已认证的用户是否包含在指定的逻辑“角色”中。  如果用户尚未通过身份验证,则该方法返回false

public String getRequestedSessionId(); // 返回客户端指定的会话ID。 该名称可能与此请求的当前有效会话的ID不同。 如果客户端未指定会话ID,则此方法返回null

public String getRequestURI(); // 返回此请求的URL的一部分,从协议名称到HTTP请求第一行中的查询字符串。 Web容器不会解码此String

public StringBuffer getRequestURL(); // 重构客户端用于发出请求的URL。 返回的URL包含协议,服务器名称,端口号和服务器路径,但不包含查询字符串参数

public String getServletPath(); // 返回此请求的URL中调用servlet的部分。 该路径以“ /”字符开头,包括servlet名称或servlet路径,但不包含任何额外的路径信息或查询字符串。

public String changeSessionId(); // 更改与此请求关联的当前会话的会话ID,并返回新的会话ID

public boolean isRequestedSessionIdValid(); // 检查请求的会话ID是否仍然有效。如果客户端未指定任何会话ID,则此方法返回false。

public boolean isRequestedSessionIdFromCookie(); // 检查请求的会话ID是否作为HTTP cookie传递到服务器。

public boolean isRequestedSessionIdFromURL(); // 检查请求的会话ID是否已作为请求URL的一部分传送到服务器。

public boolean authenticate(HttpServletResponse response) throws IOException,ServletException; // 使用为ServletContext配置的容器登录机制来验证发出此请求的用户

public void login(String username, String password) throws ServletException; // 在为ServletContext配置的Web容器登录机制所使用的密码验证领域中,验证提供的用户名和密码

public void logout() throws ServletException; // 将null设置为在请求上调用getUserPrincipal,getRemoteUser和getAuthType时返回的值

public Collection<Part> getParts() throws IOException, ServletException; // 获取此请求的所有Part组件,条件是该请求的类型为multipart / form-data

public Part getPart(String name) throws IOException, ServletException; // 获取具有给定名称的Part

ServletRequest接口

定义一个对象以向Servlet提供客户端请求信息。 Servlet容器创建一个ServletRequest对象,并将其作为参数传递给Servlet的service方法

public RequestDispatcher getRequestDispatcher(String path); // 返回一个RequestDispatcher对象,该对象充当位于给定路径的资源的包装
// 请求转发

public Object getAttribute(String name); // 将命名属性的值作为Object返回;如果不存在给定名称的属性,则返回null

public Enumeration<String> getAttributeNames(); // 返回一个枚举,其中包含可用于此请求的属性的名称

public String getCharacterEncoding(); // 返回此请求正文中使用的字符编码的名称。 如果未指定请求编码字符编码,则此方法返回null

public void setCharacterEncoding(String env) throws UnsupportedEncodingException;  // 覆盖此请求正文中使用的字符编码的名称。 在读取请求参数或使用`getReader()`读取输入之前,必须先调用此方法。 否则,它无效。

public int getContentLength(); // 返回请求主体的长度,以字节为单位,该长度可由输入流提供;如果未知,则返回-1

public long getContentLengthLong();

public String getContentType(); // 返回请求正文的MIME类型;如果类型未知,则返回null

public ServletInputStream getInputStream() throws IOException; // 使用ServletInputStream以二进制数据的形式检索请求的主体

public String getParameter(String name); // 以字符串形式返回请求参数的值,如果参数不存在,则返回null

public Enumeration<String> getParameterNames(); // 返回包含此请求中包含的参数名称的String对象的Enumeration

public String[] getParameterValues(String name); // 返回一个String对象数组,其中包含给定请求参数具有的所有值;如果该参数不存在,则返回null

public Map<String, String[]> getParameterMap(); // 返回此请求的参数的java.util.Map

public String getProtocol(); // 返回请求使用的协议的名称和版本,例如HTTP / 1.1

public String getScheme(); // 返回用于发出此请求的方案的名称,例如,http,https或ftp

public String getServerName(); // 返回请求发送到的服务器的主机名。

public int getServerPort(); // 返回请求发送到的端口号

public BufferedReader getReader() throws IOException; // 使用BufferedReader检索请求的正文作为字符数据

public String getRemoteAddr(); // 返回发送请求的客户端或最后一个代理的Internet协议(IP)地址

public String getRemoteHost(); // 返回发送请求的客户端或最后一个代理的标准名称

public int getRemotePort(); // 返回发送请求的客户端或最后一个代理的Internet协议(IP)源端口。

public void setAttribute(String name, Object o); // 在此请求中存储属性。 在请求之间重置属性。 此方法最常与RequestDispatcher结合使用

public void removeAttribute(String name); // 从此请求中删除属性。 通常不需要此方法,因为仅在处理请求时属性才持久存在。

public Locale getLocale(); // 根据Accept-Language标头返回客户端将在其中接受内容的首选语言环境。 如果客户端请求未提供Accept-Language标头,则此方法返回服务器的默认语言环境。

public Enumeration<Locale> getLocales(); // 返回一个Locale对象的Enumeration,以从首选语言环境开始的降序指示,基于Accept-Language标头,客户端可以接受的语言环境。

public boolean isSecure(); // 返回一个布尔值,指示此请求是否使用安全通道(例如HTTPS)发出

public String getLocalName(); // 返回接收请求的Internet Protocol (IP)接口的主机名

public String getLocalAddr(); // 返回接收请求的接口的Internet协议(IP)地址

public int getLocalPort(); // 返回接收请求的接口的Internet协议(IP)端口号

public ServletContext getServletContext(); // 获取servlet请求最后被分派到的servlet上下文

public AsyncContext startAsync() throws IllegalStateException; // 将此请求置于异步模式

public boolean isAsyncStarted(); // 检查此请求是否已置于异步模式

public boolean isAsyncSupported(); // 检查此请求是否支持异步操作

public AsyncContext getAsyncContext(); // 获取通过对此请求最近一次调用startAsync或startAsync(ServletRequest,ServletResponse)创建或重新初始化的AsyncContext。

public DispatcherType getDispatcherType(); // 获取此请求的调度程序类型

获取表单值

JSP

<body>
    <form action="<%=request.getContextPath()%>/getFormValue" method="get" name="info">
        <p>用户名:<input type="text" name="username"></p>
        <p>密码:<input type="password" name="password"></p>
        <p>性别:
            <input type="radio" name="gender" value="man" checked>男
            <input type="radio" name="gender" value="woman">女
        </p>
        <p>爱好:
            <input type="checkbox" name="hobbies" value="eat">吃
            <input type="checkbox" name="hobbies" value="drink">喝
        </p>
        <p>居住地:
            <select name="city">
                <option value="beijing">北京</option>
                <option value="shanghai">上海</option>
            </select>
        </p>
        <p><input type="submit" value="提交"><input type="reset"></p>
    </form>
</body>

Servlet

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // request.setCharacterEncoding("utf-8");
    Enumeration<String> parameterNames = request.getParameterNames();
    while (parameterNames.hasMoreElements()) {
        // username、password、gender、hobbies、city
        System.out.println(parameterNames.nextElement());
    }

    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String gender = request.getParameter("gender");
    String[] hobbies = request.getParameterValues("hobbies");
    String city = request.getParameter("city");

    System.out.println("username:"+ username);
    System.out.println("password:"+ password);
    System.out.println("gender:"+ gender);
    System.out.println("hobbies:"+ Arrays.toString(hobbies));
    System.out.println("city:"+ city);
}

tomcat8.0开始,URIEncoding默认值不再是ISO8859-1,而变成了UTF-8,Get请求方式不用再转换编码,post方式仍需要

原文地址:https://www.cnblogs.com/shenleg/p/14252305.html