Request功能
1.获取请求消息数据
- 获取请求行数据
- 获取请求头数据
- 获取请求体数据
请求空行没必要获取
1.获取请求行数据
GET /虚拟目录 /servlet路径 ?请求参数 HTTP/1.1
GET/day14/demo1?username=zhangsan
方法:
1.获取请求方式:GET
String getMethod()
2.获取虚拟目录:/day14。常用
String getContextPath()
3.获取servlet路径:/demo1
String getServletPath()
4.获取get方式的请求参数:username=zhangsan
String getQueryString()
5.获取请求的uri:/day14/demo1。常用
String getRequestURI():返回/day14/demo1
String getRequestURL():返回http://localhoust/day14/demo1
6.获取协议和版本:HTTP/1.1
String getProtocol()从ServletRequest继承而来
7.获取客户机的IP地址
String getRemoteAddr()从ServletRequest继承而来
可以帮你继承HttpServlet复写两个方法
package cn.itcast.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 旗木五五开
* @create 2020-02-22 12:55
* 演示
*/
@WebServlet("/requestServlet")
public class RequestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
//http://localhost:8080/ji/requestServlet?name=zhang&age=12
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.获取请求方式:GET
// 可以来判断请求方,根据请求方式不同,做出不同的代码逻辑处理
// HttpServlet内部已经写好了
String method = request.getMethod();
System.out.println(method);
// 2.获取虚拟目录:/day14
String contextPath = request.getContextPath();
System.out.println(contextPath);
// 3.获取servlet请求路径:/requestServlet
String servletPath = request.getServletPath();
System.out.println(servletPath);
// 4.获取get方式的请求参数:name=zhang&age=12
// 有更高级的
String queryString = request.getQueryString();
System.out.println(queryString);
// 5.获取请求的uri:
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
// /ji/requestServlet
System.out.println(requestURI);
// http://localhost:8080/ji/requestServlet
System.out.println(requestURL);
// 6.获取协议和版本:HTTP/1.1
String protocol = request.getProtocol();
System.out.println(protocol);
// 7.获取客户机的IP地址:0:0:0:0:0:0:0:1
String remoteAddr = request.getRemoteAddr();
System.out.println(remoteAddr);
}
}
2.获取请求头数据常用
方法
String getHeader(String name):通过请求头的名称获取请求头的值。常用
Enumeration<String> getHeaderNames():获取所有请求头的名称
package cn.itcast.web.request;
import jdk.nashorn.internal.objects.annotations.Where;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
/**
* @author 旗木五五开
* @create 2020-02-22 14:04
* 获取所有请求头数据
*/
@WebServlet("/requestDemo2")
public class RequestDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取所有请求头名称
Enumeration<String> headerNames = request.getHeaderNames();
// 遍历
while (headerNames.hasMoreElements()) {
// 获取请求头名称
String name = headerNames.nextElement();
// 获取请求头对应的值
String value = request.getHeader(name);
System.out.println(name+":"+value);
}
}
}
package cn.itcast.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 旗木五五开
* @create 2020-02-22 17:05
* user-agent访问的浏览器版本信息
*/
@WebServlet("/RequestDemo3")
public class RequestDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 演示获取请求头数据user-agent
// 不区分大小写
String agent = request.getHeader("user-agent");
// 判断浏览器版本,不同版本不同操作解决浏览器兼容问题
// contains判断是否存在包含关系
if(agent.contains("Chrome")){
// 谷歌
System.out.println("谷歌");
}else if(agent.contains("Firefox")){
// 火狐
System.out.println("火狐");
}
}
}
referer请求从哪里来
没有从别的超链接,点链接来访问 直接从浏览器地址栏输入 无法获取 因为我没有从哪里来,直接访问的referer 获取的是null不能直接访问,用超链接来实现 返回:http://localhost:8080/ji/login.html
package cn.itcast.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 旗木五五开
* @create 2020-02-23 12:35
* referer请求从哪里来
*/
@WebServlet("/requestDemo4")
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 演示获取请求头数据:referer请求从哪里来
String referer = request.getHeader("referer");
System.out.println(referer);
/*
没有从别的超链接,点链接来访问
直接从浏览器地址栏输入
无法获取
因为我没有从哪里来,直接访问的referer
获取的是null
不能直接访问,用超链接来实现
返回:http://localhost:8080/ji/login.html
*/
//防盗链
if (referer != null){
if (referer.contains("ji")){
// 正常访问
System.out.println("播放");
}else{
// 盗链
System.out.println("请访问优酷");
}
}
}
}
创建另一个项目访问当前项目
http://localhost:8080/ji/requestDemo4
这个是直接访问referer的路径
这时候Tomcat默认部署(发布)的就是hehe项目
所以要新建一个Tomcat来部署(发布)servlet项目
不能有两个8080端口
启动hehe项目
启动servlet项目
从高清电影访问referer
在页面上显示
package cn.itcast.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 旗木五五开
* @create 2020-02-23 12:35
* referer请求从哪里来
*/
@WebServlet("/requestDemo4")
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 演示获取请求头数据:referer请求从哪里来
String referer = request.getHeader("referer");
System.out.println(referer);
/*
没有从别的超链接,点链接来访问
直接从浏览器地址栏输入
无法获取
因为我没有从哪里来,直接访问的referer
获取的是null
不能直接访问,用超链接来实现
http://localhost:8080/ji/login.html
*/
//防盗链
if (referer != null){
if (referer.contains("ji")){
// 正常访问
System.out.println("播放");
// 页面上显示
// 如果要写中文
response.setContentType("text/html;charset=utf-8");
// 页面上显示的内容
response.getWriter().write("播放");
}else{
// 盗链
System.out.println("请访问优酷");
// 页面上显示
// 如果要写中文
response.setContentType("text/html;charset=utf-8");
// 页面上显示的内容
response.getWriter().write("请访问优酷");
}
}
}
}
hehe项目的高清电影访问
servlet上的login访问
3.获取请求体数据
只有POST请求方式才有请求体,在请求体post中封装了请求参数
request对象将请求体数据封装成流
步骤:
- 获取流对象
- 再从流东西中拿数据
获取流对象的方法:
字符流:BufferedReader getReader():只能操作字符数据
字节流:ServletInputStream getInputStream():可以操作所有类型数据,继承了InputStream,可以把它当做InputStream来用。
文件上传是写
package cn.itcast.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
/**
* @author 旗木五五开
* @create 2020-02-24 12:14
* getReader()案例
*/
@WebServlet("/requestDemo5")
public class RequestDemo5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.获取请求消息体——请求参数
BufferedReader br = request.getReader();
// 2.读取数据
String line = null;
while ((line = br.readLine())!=null){
System.out.println(line);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
post参数的格式和get参数的格式是一样的,只不过存放的位置不一样。
常用的方法
获取虚拟目录:/day14。
String getContextPath()
获取请求的uri:/day14/demo1。
String getRequestURI():返回/day14/demo1
String getRequestURL():返回http://localhoust/day14/demo1
String getHeader(String name):通过请求头的名称获取请求头的值。
2.其他功能:重要
基于第一类方法生成的,更方便使用request对象的方法
1.获取请求参数——通用方式(可以兼容get和post)常用
1.String getParameter(String name):根据参数名称获取参数值。内部是通过流来获取的参数的值
例如:username=zhang&password=123,传username返回zhang,传password返回123
package cn.itcast.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 旗木五五开
* @create 2020-02-24 14:33
* 获取请求参数
* 1.String getParameter(String name):根据参数名称获取参数值
*/
@WebServlet("/requestDemo6")
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// post获取请求参数
// getParameter根据参数名称获取参数值
String username = request.getParameter("username");
System.out.println("post");
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// get获取请求参数
// String username = request.getParameter("username");
// System.out.println("get");
// System.out.println(username);
this.doPost(request,response);
}
}
2.String[] getParameterValues(String name):根据参数名称获取参数值的数组。
例如hobby=xuexi&hobby=game,通过一个键可以获取两个值,把两个值封装成了一个数组。多用于复选框
package cn.itcast.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 旗木五五开
* @create 2020-02-24 14:33
* 获取请求参数
* 2.String[] getParameterValues(String name):根据参数名称获取参数值的数组。
*/
@WebServlet("/requestDemo6")
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 根据参数名称获取参数值的数组。
String[] hobbies = request.getParameterValues("hobby");
for (String hobby : hobbies) {
System.out.println(hobby);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
3.Enumeration<String> getParametNames():获取请求参数的名称的枚举
方法是将发送请求页面中form表单里所有具有name属性的表单对象获取(包括button).返回一个Enumeration类型的枚举.
通过Enumeration的hasMoreElements()方法遍历.再由nextElement()方法获得枚举的值.此时的值是form表单中所有控件的name属性的值.
最后通过request.getParameter()方法获取表单控件的value值.
package cn.itcast.web.request;
import javax.naming.Name;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
/**
* @author 旗木五五开
* @create 2020-02-24 14:33
* 获取请求参数
* 3.Enumeration<String> getParametNames():获取请求参数的名称的枚举
*/
@WebServlet("/requestDemo6")
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()){
String name = parameterNames.nextElement();
System.out.println(name);
String value = request.getParameter(name);
System.out.println(value);
System.out.println("-------------");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
没有获取到学习
4.Map<String,String[]> getParameterMap():获取所有参数的map集合。常用
键是字符串,值是字符串数组。是为了防止上面那种情况,复选框只能获取一个值
package cn.itcast.web.request;
import javax.naming.Name;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
/**
* @author 旗木五五开
* @create 2020-02-24 14:33
* 获取请求参数
* 4.Map<String,String[]> getParameterMap():获取所有参数的map集合
*/
@WebServlet("/requestDemo6")
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取所有参数的map集合
Map<String, String[]> parameterMap = request.getParameterMap();
// 遍历
Set<String> keyset = parameterMap.keySet();
for (String name : keyset) {
// 根据键获取值
String[] values = parameterMap.get(name);
System.out.println(name);
for (String value : values) {
System.out.println(value);
}
System.out.println("--------------");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
解决请求参数中文乱码
get方式:Tomcat8已经将乱码问题解决了
post方式:会乱码
解决方式:设置流的编码。
以后页面多的时候无论get还是post在获取请求参数的时候就写上这个代码
request.setCharacterEncoding("utf-8");
package cn.itcast.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 旗木五五开
* @create 2020-02-27 11:08
*/
@WebServlet("/requestDemo7")
public class RequestDemo7 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.设置流的编码
request.setCharacterEncoding("utf-8");
// 获取请求参数username
String username = request.getParameter("username");
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
utf-8从哪里来的?
2.请求转发:一种在服务器内部的资源跳转方式
程序设计要求:功能单一,细化。不要把很多代码写到一个类里,方法里。不利于后期维护,不利于分工协作
请求转发就是为了这个,A做完,丢给B
步骤:
1.通过request对象,获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
2.使用RequestDispatcher 对象转发:forward(ServletRequest request, ServletResponse response)
package cn.itcast.web.request;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 旗木五五开
* @create 2020-02-27 13:47
*/
@WebServlet("/requestDemo8")
public class RequestDemo8 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("8被访问了");
// 转发到demo9资源
/*
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo9");
requestDispatcher.forward(request,response);
不要这样写,把代码合成一行。
因为requestDispatcher只用一次,只调用一次forward。这样写要单独创建变量不合算
*/
request.getRequestDispatcher("/requestDemo9").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
特点:
1.浏览器地址栏不发生变化
2.只能转发到服务器的内部资源中
3.转发是一次请求
3.共享数据
A做完丢给B的时候,需要告诉B干到哪了,之间需要数据通信的
转发的时候才使用
步骤:
1.存储数据到request域中
2.转发
3.获取
package cn.itcast.web.request;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 旗木五五开
* @create 2020-02-27 13:47
*/
@WebServlet("/requestDemo8")
public class RequestDemo8 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("8被访问了");
// 1.存储数据到request域中
request.setAttribute("msg","hello");
// 2.转发
request.getRequestDispatcher("/requestDemo9").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
package cn.itcast.web.request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 旗木五五开
* @create 2020-02-27 13:47
*/
@WebServlet("/requestDemo9")
public class RequestDemo9 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("9被访问了");
// 获取数据
Object msg = request.getAttribute("msg");
System.out.println(msg);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
4.获取ServletContext
ServletContext getServletContext()
package cn.itcast.web.request;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 旗木五五开
* @create 2020-02-27 14:31
*/
@WebServlet("/requestDemo10")
public class RequestDemo10 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = request.getServletContext();
System.out.println(servletContext);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}