Request January 27,2020
Posted yyanghang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Request January 27,2020相关的知识,希望对你有一定的参考价值。
## Request:
一、request对象和response对象的原理
1. request和response对象是由服务器创建的。我们来使用它们
2. request对象是来获取请求消息,response对象是来设置响应消息
二、request对象继承体系结构:
ServletRequest -- 接口
| 继承
HttpServletRequest -- 接口
| 实现
org.apache.catalina.connector.RequestFacade 类(tomcat)
三、request功能:
1. 获取请求消息数据
1. 获取请求行数据
* GET /day14/demo1?name=zhangsan HTTP/1.1
* 方法:
1. 获取请求方式 :GET
* String getMethod()
2. (*)获取虚拟目录:/day14
* String getContextPath()
3. 获取Servlet路径: /demo1
* String getServletPath()
4. 获取get方式请求参数:name=zhangsan
* String getQueryString()
5. (*)获取请求URI:/day14/demo1
* String getRequestURI(): /day14/demo1
* StringBuffer getRequestURL() :http://localhost/day14/demo1
* URL:统一资源定位符 : http://localhost/day14/demo1 中华人民共和国
* URI:统一资源标识符 : /day14/demo1 共和国
6. 获取协议及版本:HTTP/1.1
* String getProtocol()
7. 获取客户机的IP地址:
* String getRemoteAddr()
2. 获取请求头数据
* 方法:
* (*)String getHeader(String name):通过请求头的名称获取请求头的值
* Enumeration<String> getHeaderNames():获取所有的请求头名称
/** * 演示获取请求头数据 */ @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 { //1.获取所有请求头名称 Enumeration<String> headerNames = request.getHeaderNames(); //2.遍历 while (headerNames.hasMoreElements()){ String name = headerNames.nextElement(); //根据名称获取请求头的值 String value = request.getHeader(name); System.out.println(name+"---"+value); } } }/** * 演示获取请求头数据 user-agent 判断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 { String agent = request.getHeader("user-agent"); //判断 if(agent.contains("Chrome")){ System.out.println("谷歌来了"); }else { System.out.println("你就不能多安个浏览器吗?"); } } }
/** * 演示获取请求头数据 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 { String referer = request.getHeader("referer"); System.out.println(referer); //防盗链 if(referer!=null){ //正常访问 if(referer.contains("/order7_Request_Respons_war_exploded")){ System.out.println("尊敬的会员用户,你的视频来源是合法的"); }else { //盗链 System.out.println("对不起,你是从非法网站访问过来的,无法观看"); } } } }
3. 获取请求体数据:
* 请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数
* 步骤:
1. 获取流对象
* BufferedReader getReader():获取字符输入流,只能操作字符数据
* ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据
2. 再从流对象中拿数据
/** * 演示获取请求体--请求参数 */ @WebServlet("/requestDemo5") public class RequestDemo5 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //读取字符流 BufferedReader reader = request.getReader(); String line = null; //读取数据 while ((line = reader.readLine()) != null){ System.out.println(line); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
2. 其他功能:
1. 获取请求参数通用方式:不论get还是post请求方式都可以使用下列方法来获取请求参数
1. String getParameter(String name):根据参数名称获取参数值 username=zs&password=123
2. String[] getParameterValues(String name):根据参数名称获取参数值的数组 hobby=xx&hobby=game
3. Enumeration<String> getParameterNames():获取所有请求的参数名称
4. Map<String,String[]> getParameterMap():获取所有参数的map集合
* 中文乱码问题:
* get方式:tomcat 8 已经将get方式乱码问题解决了
* post方式:会乱码
* 解决:在获取参数前,设置request的编码request.setCharacterEncoding("utf-8");
/** * 演示post获取请求参数 * request获取请求参数以及中文乱码问题 */ @WebServlet("/requestDemo6") public class RequestDemo6 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //防止出现中文乱码 request.setCharacterEncoding("UTF-8"); //根据参数名称获取参数值 String username = request.getParameter("username"); System.out.println(username); System.out.println("------------"); //根据参数名称 获取参数值的数组 String[] value = request.getParameterValues("hobby"); for (String name : value) { System.out.println(name); } System.out.println("------------"); //获取所有请求的参数名称 Enumeration<String> parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()){ String s = parameterNames.nextElement(); System.out.println(s); //只能获取一个 获取多个需要使用getParameterMap String parameter = request.getParameter(s); System.out.println(parameter); } System.out.println(); Map<String, String[]> parameterMap = request.getParameterMap(); for (String name : parameterMap.keySet()) { //根据键来获取值 String[] strings = parameterMap.get(name); System.out.println(name); for (String nameValue : strings) { System.out.println(nameValue); } System.out.println("---------"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //演示post获取请求参数 this.doPost(request,response); } }
2. 请求转发:一种在服务器内部的资源跳转方式
1. 步骤:
1. 通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
2. 使用RequestDispatcher对象来进行转发:forward(ServletRequest request, ServletResponse response)
2. 特点:
1. 浏览器地址栏路径不发生变化
2. 只能转发到当前服务器内部资源中。
3. 转发是一次请求
3. 共享数据:
* 域对象:一个有作用范围的对象,可以在范围内共享数据
* request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据
* 方法:
1. void setAttribute(String name,Object obj):存储数据
2. Object getAttitude(String name):通过键获取值
3. void removeAttribute(String name):通过键移除键值对
/** * 请求转发和request共享数据 */ @WebServlet("/requestDemo7") public class RequestDemo7 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //7 和 8用来演示资源跳转 System.out.println("资源跳转----demo7来了"); //资源共享 存储数据 request.setAttribute("zygx","资源共享了"); //跳转 /* RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo8"); requestDispatcher.forward(request,response); */ request.getRequestDispatcher("/requestDemo8").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //演示post获取请求参数 this.doPost(request,response); } } @WebServlet("/requestDemo8") public class RequestDemo8 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //7 和 8用来演示资源跳转 System.out.println("资源跳转----demo8来了"); //通过键读取数据 Object dy = request.getAttribute("zygx"); System.out.println(dy); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //演示post获取请求参数 this.doPost(request,response); } }
4. 获取ServletContext:
* ServletContext getServletContext()
以上是关于Request January 27,2020的主要内容,如果未能解决你的问题,请参考以下文章