Request:
-
request对象和response对象的原理
1. request和response对象是由服务器创建的。我们来使用它们
2. request对象是来获取请求消息,response对象是来设置响应消息 -
request对象继承体系结构:
- ServletRequest – 接口
| 继承 - HttpServletRequest – 接口
| 实现 - org.apache.catalina.connector.RequestFacade 类(tomcat)
- ServletRequest – 接口
-
request功能:
- 获取请求消息数据
- 获取请求行数据
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()
@WebServlet("/RequestDemo1") public class RequestDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取请求方式 String method = request.getMethod(); System.out.println(method); //获取虚拟目录 String contextPath = request.getContextPath(); System.out.println(contextPath); //获取Servlet路径 String servletPath = request.getServletPath(); System.out.println(servletPath); //获取get方式请求参数,name=zhangsan String queryString = request.getQueryString(); System.out.println(queryString); //获取请求URI String requestURI = request.getRequestURI(); System.out.println(requestURI); //获取请求URL StringBuffer requestURL = request.getRequestURL(); System.out.println(requestURL); //获取协议及版本 String protocol = request.getProtocol(); System.out.println(protocol); //获取客户机的IP地址 String remoteAddr = request.getRemoteAddr(); System.out.println(remoteAddr); } }
- 方法:
- 获取请求头数据
- 方法:
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 { //获取请求 //获取所有请求头名称 Enumeration<String> headerNames = request.getHeaderNames(); //遍历 while(headerNames.hasMoreElements()){ String name = headerNames.nextElement(); String value = request.getHeader(name); System.out.println(name+"----"+value); } //获取请求 //获取所有请求头名称 user-agent String agent = request.getHeader("user-agent"); //判断agent的浏览器版本 if(agent.contains("Chrome")) System.out.println("谷歌来了"); else if(agent.contains("Firefox")) { System.out.println("火狐来了"); } //获取请求 //获取所有请求头名称 referer String referer = request.getHeader("referer"); System.out.println(referer); //反盗链 if(referer != null) { if(referer.contains("/java03")){ //正常访问 System.out.println("播放电影"); }else { //盗链 System.out.println("想看电影吗"); } } } }
- 方法:
- 获取请求体数据:
- 请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数
- 步骤:
- 获取流对象
BufferedReader getReader():获取字符输入流,只能操作字符数据
ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据
- 再从流对象中拿数据
- 获取流对象
- 步骤:
@WebServlet("/requestDemo5") public class RequestDemo5 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取请求消息体 //1 获取字符流 BufferedReader br = request.getReader(); String line = null; while((line = br.readLine()) != null){ System.out.println(line); } //post获取请求参数 //根据参数名称获取参数值 String username = request.getParameter("username"); System.out.println("post"); System.out.println(username); //根据参数名称获取参数值的数组----获取复选框的值 String[] hobbies = request.getParameterValues("hobby"); for(String hobby: hobbies){ System.out.println(hobby); } //获取所有参数的请求名称 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("----------------------"); } //获取所有参数的map集合 Map<String, String[]> paramererMap = request.getParameterMap(); //遍历 Set<String> keySet = paramererMap.keySet(); for(String name: keySet){ System.out.println(name); String[] values = paramererMap.get(name); for (String value : values) { System.out.println(value); } System.out.println("==========================="); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //get获取请求参数 //根据参数名称获取参数值 String username = request.getParameter("username"); System.out.println("get"); System.out.println(username); } }
- 请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数
- 获取请求行数据
- 获取请求消息数据
-
其他功能:
-
获取请求参数通用方式:不论get还是post请求方式都可以使用下列方法来获取请求参数
String getParameter(String name):根据参数名称获取参数值 username=zs&password=123
String[] getParameterValues(String name):根据参数名称获取参数值的数组 hobby=xx&hobby=game
Enumeration<String> getParameterNames():获取所有请求的参数名称
Map<String,String[]> getParameterMap():获取所有参数的map集合
-
中文乱码问题:
* get方式:tomcat 8 已经将get方式乱码问题解决了
* post方式:会乱码
* 解决:在获取参数前,设置request的编码request.setCharacterEncoding("utf-8");
-
请求转发:一种在服务器内部的资源跳转方式
-
步骤:
1. 通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
2. 使用RequestDispatcher对象来进行转发:forward(ServletRequest request, ServletResponse response)
-
特点:
1. 浏览器地址栏路径不发生变化
2. 只能转发到当前服务器内部资源中。
3. 转发是一次请求
-
-
@WebServlet("/requestDemo7")
public class RequestDemo7 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post获取请求参数username
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post获取请求参数username
String username = request.getParameter("username");
System.out.println(username);
}
}
-
请求转发:一种在服务器内部的资源跳转方式
-
步骤:
1. 通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
2. 使用RequestDispatcher对象来进行转发:forward(ServletRequest request, ServletResponse response)
-
特点:
1. 浏览器地址栏路径不发生变化
2. 只能转发到当前服务器内部资源中。
3. 转发是一次请求
-
-
共享数据:
- 域对象:一个有作用范围的对象,可以在范围内共享数据
- request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据
- 方法:
1. void setAttribute(String name,Object obj):存储数据
2. Object getAttitude(String name):通过键获取值
3. void removeAttribute(String name):通过键移除键值对
-
获取ServletContext:
* ServletContext getServletContext()
@WebServlet("/requestDemo8")
public class RequestDemo8 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo8888");
request.setAttribute("name","hehehheh");
//转发到demo9
request.getRequestDispatcher("/requestDemo9").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
@WebServlet("/requestDemo9")
public class RequestDemo9 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request.getAttribute("name"));
System.out.println("demo99999999");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}