HttpServletRequest接口
public interface HttpServletRequest
extends ServletRequest
Extends the ServletRequest interface to provide request information for HTTP servlets.
The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet's service methods (doGet, doPost, etc).
1、javax.servlet.http.HttpServletRequest是Servlet规范中的一员
2、javax.servlet.http.HttpServletRequest是接口
3、javax.servlet.http.HttpServletRequest表示请求
4、请求对应的英语单词是:request
5、javax.servlet.http.HttpServletRequest接口的继承关系:
- javax.servlet.ServletRequest【父接口】
- javax.servlet.http.HttpServletRequest【子接口】
6、request对象生命周期是多久?
* 一次请求对应一个request对象
* 两个不同的请求,会分别对应两个不同的request对象
7、request对象中封装了什么信息呢?
* HTTP的请求协议的全部内容,被自动封装到了request对象当中。
所以面向request接口可以获取请求协议中的所有信息。
8、什么是一次请求?
* 浏览器向服务器发送了一个URL,就会对应一次请求。
9、Tomcat服务器对HttpServletRequest接口的实现类完整类名:org.apache.catalina.connector.RequestFacade
package org.apache.catalina.connector;
public class RequestFacade implements javax.servlet.http.HttpServletRequest{
//实现体
}
对于我们javaweb程序员来说,面向HttpServletRequest接口调用方法即可。面向接口编程。
10、javax.servlet.http.HttpServletRequest接口中常用的方法:
获取表单参数问题
* String getParameter(String name)
* String[] getParameterValues(String name)
* Map getParameterMap()
* Enumeration getParameterNames()
* String getRemoteAddr()
* String getContextPath()
* String getMethod()
* String getRequestURI()
* StringBuffer getRequestURL()
* String getServletPath()
* void setAttribute(String name, Object o)
* Object getAttribute(String name)
* void removeAttribute(String name)
* RequestDispatcher getRequestDispatcher(String path)
* void setCharacterEncoding(String env)针对post请求方式:解决post请求乱码,设置请求体中的编码方式
* Cookie[] getCookies()
* HttpSession getSession()
11、以下的方法在ServletContext【application】和HttpServletRequest【request】中都有:
* void setAttribute(String name, Object o) 存【绑定】
* Object getAttribute(String name) 取
* void removeAttribute(String name) 删
ServletContext application;【应用范围对象】
HttpServletRequest request;【请求范围对象】
application > request
application 对于同一个webapp来说只有一个,范围最大
request 对于一个请求一个request对象,范围很小
使用原则:
优先使用小范围对象,小范围对象无法满足需求,再选择更大的范围对象。
因为越小的范围对象,占用资源越少。
request是请求范围对象,只能完成在同一次请求当中传递或共享数据。
12、怎么做从AServlet跳转到BServlet执行呢?
* 可以依靠转发机制
* 实现转发:
RequestDispatcher dispatcher = request.getRequestDispatcher("/b"); //getRequestDispatcher是request中的方法
dispatcher.forward(request,response); //forward是RequestDispatcher中的方法
* 一步搞定:
request.getRequestDispatcher("/b").forward(request,response);
具体验证代码如下:
简单编写提交页面index.html:
<html>
<head>
<title>登录页面</title>
<meta charset="UTF-8">
</head>
<body>
<!-- HttpServletRequest -> 获取请求的信息 -->
<a href="/httpServletRequest/httpServletRequestDemo">测试request</a>
<!-- HttpServletRequest -> 获取表单提交信息 -->
<form action="/httpServletRequest/httpServletRequestDemo1" method="post">
用户名
<input type="text" name="username">
<br>
密码
<input type="password" name="password">
<br>
性别
<input type="radio" name="sex" value="m"> 男
<input type="radio" name="sex" value="f"> 女
<br>
爱好
<input type="checkbox" name="aihao" value="sport">运动
<input type="checkbox" name="aihao" value="music">音乐
<input type="checkbox" name="aihao" value="food">美食
<input type="checkbox" name="aihao" value="sleep">睡觉
<input type="checkbox" name="aihao" value="travel">旅游
<br>
学历
<select name="xueli">
<option value="gz">高中</option>
<option value="dz">大专</option>
<option value="db">大本</option>
</select>
<br>
简介
<textarea rows="10" cols="60" name="jianjie"></textarea>
<br>
<input type="submit" value="注册">
</form>
</body>
</html>
编写web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- HttpServletRequest -> 获取请求的信息 -->
<servlet>
<servlet-name>HttpServletRequestDemo</servlet-name>
<servlet-class>com.httpServletRequest.HttpServletRequestDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HttpServletRequestDemo</servlet-name>
<url-pattern>/httpServletRequestDemo</url-pattern>
</servlet-mapping>
<!-- HttpServletRequest -> 获取表单提交信息 -->
<servlet>
<servlet-name>HttpServletRequestDemo1</servlet-name>
<servlet-class>com.httpServletRequest.HttpServletRequestDemo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HttpServletRequestDemo1</servlet-name>
<url-pattern>/httpServletRequestDemo1</url-pattern>
</servlet-mapping>
<!-- HttpServletRequest -> 转发 -->
<servlet>
<servlet-name>httpServletRequestDemo1Forward</servlet-name>
<servlet-class>com.httpServletRequest.HttpServletRequestDemo1Forward</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>httpServletRequestDemo1Forward</servlet-name>
<url-pattern>/httpServletRequestDemo1/httpServletRequestDemo1Forward</url-pattern>
</servlet-mapping>
</web-app>
Servlet获取表单提交信息:
package com.httpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* HttpServletRequest中获取表单提交信息方法
* @author cheng_xy
*
*/
public class HttpServletRequestDemo1 extends javax.servlet.http.HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//[email protected]
System.out.println("request = " + request); //一次请求对应一个request对象
/*
表单提交了以下的数据:username=admin&password=123&sex=m&aihao=sport&aihao=music&aihao=food&xueli=db&jianjie=adminok
以上浏览器提交的数据被web服务器自动封装到request对象当中,request对象中有一个Map集合专门用来存储以上的数据:Map<String,String[]>
Map<String,String[]>
key value
------------------------------------
"username" {"admin"}
"password" {"123"}
"sex" {"m"}
"aihao" {"sport","music","food"}
"xueli" {"db"}
"jianjie" {"adminok"}
*/
System.out.println("----------------request.getParameter开始----------------");
//通过key获取一维数组中的第一个元素【使用最多,因为大部分情况下一维数组中只有一个元素】
String username = request.getParameter("username");
System.out.println("username = " + username);
System.out.println("----------------request.getParameter完成----------------");
System.out.println("----------------request.getParameterValues开始----------------");
//遇到复选框的提交数据的时候,或者说遇到“多个name是相同”的键值对提交数据的时候
//调用以下方法,通过key获取一维数组
String[] aihaos = request.getParameterValues("aihao");
System.out.println(aihaos);
if(aihaos != null && aihaos.length > 0){
for(String aihao : aihaos){
System.out.println(aihao);
}
}
String aihao0 = request.getParameter("aihao");
System.out.println(aihao0);
System.out.println("----------------request.getParameterValues完成----------------");
System.out.println("----------------request.getParameterMap开始----------------");
//获取参数Map集合,使用较少
Map<String,String[]> parameterMap = request.getParameterMap();
//System.out.println(parameterMap);
Set<String> keys = parameterMap.keySet();
for(String key : keys){
String[] value = parameterMap.get(key);
System.out.println(key + "=" + value);
}
System.out.println("----------------request.getParameterMap完成----------------");
System.out.println("----------------request.getParameterNames开始----------------");
//获取所有的参数name
Enumeration<String> parameterNames = request.getParameterNames();
//System.out.println(parameterNames);
while(parameterNames.hasMoreElements()){
String parameterName = parameterNames.nextElement();
System.out.println(parameterName);
}
System.out.println("----------------request.getParameterNames完成----------------");
System.out.println("----------------request获取表单中所有数据,开始----------------");
//获取表单中提交的所有数据
String username1 = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String[] aihaos1 = request.getParameterValues("aihao");
String xueli = request.getParameter("xueli");
String jianjie = request.getParameter("jianjie");
System.out.println("username=" + username1);
System.out.println("password=" + password);
System.out.println("sex=" + sex);
if(aihaos1 != null && aihaos1.length > 0){
for(String aihao : aihaos1){
System.out.print("aihao=" + aihao + " ");
}
}else{
System.out.print("aihao=" + aihaos1);
}
System.out.println();
System.out.println("xueli=" + xueli);
System.out.println("jianjie=" + jianjie);
System.out.println("----------------request获取表单中所有数据,完成----------------");
}
}
HttpServletRequest的一些方法与转发:
package com.httpServletRequest;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HttpServletRequestDemo extends javax.servlet.http.HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* String getRemoteAddr()
* String getContextPath()
* String getMethod()
* String getRequestURI()
* StringBuffer getRequestURL()
* String getServletPath()
*/
//获取客户端IP地址
String ip = request.getRemoteAddr();
System.out.println("发送请求的客户端IP地址是:" + ip);
//获取上下文根路径
String contextPath = request.getContextPath();
System.out.println("contextPath = " + contextPath);
//获取请求方式:get/post
String method = request.getMethod();
System.out.println(method);
//获取uri
String uri = request.getRequestURI();
System.out.println(uri);
//获取url
String url = request.getRequestURL().toString();
System.out.println(url);
//获取servletPath
String servletPath = request.getServletPath();
System.out.println(servletPath);
//将上面的bean对象绑定到request范围当中:request是一个范围对象,但是代表请求范围。很小。
request.setAttribute("forward", "forward");
//当AServlet执行到最后的时候,转发到BServlet继续执行本次请求
//转发机制是一次请求:forward
//怎么完成转发?
//分两步:
//第一步:获取请求转发器对象
//RequestDispatcher dispatcher = request.getRequestDispatcher("/b"); //请求转发器指向下一个资源
//第二步:调用转发器中的forward方法完成转发
//dispatcher.forward(request, response);
//转发
request.getRequestDispatcher("/httpServletRequestDemo1/httpServletRequestDemo1Forward").forward(request, response);
}
}
package com.httpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* HttpServletRequest中获取表单提交信息方法
*/
public class HttpServletRequestDemo1Forward extends javax.servlet.http.HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//[email protected]
System.out.println("request = " + request); //一次请求对应一个request对象
System.out.println("Method = " + request.getMethod());
//从request范围中读取绑定的数据
Object obj = request.getAttribute("forward");
//将取出的数据输出到浏览器
response.setContentType("text/html;charset=UTF-8");
response.getWriter().print(obj);
}
}
希望对你有帮助,祝你有一个好心情,加油!
若有错误、不全、可优化的点,欢迎纠正与补充!