resquest对象 + ServletContext对象
一、request对象和response对象的原理
request对象和response对象的原理
1. request和response对象是由服务器创建的。我们来使用它们
2. request对象是来获取请求消息,response对象是来设置响应消息
二、request对象继承体系结构:
ServletRequest -- 接口
| 继承
HttpServletRequest -- 接口
| 实现
org.apache.catalina.connector.RequestFacade 类(tomcat)
三、request功能
1.Request 获取请求行数据
格式:GET /day14/demo1?name=zhangsan HTTP/1.1
方法:
-
获取请求方式 :GET
String getMethod() -
(*)获取虚拟目录:/day14
String getContextPath() -
获取Servlet路径: /demo1
String getServletPath() -
获取get方式请求参数:name=zhangsan
String getQueryString() -
(*)获取请求URI:/day14/demo1
String getRequestURI(): /day14/demo1
StringBuffer getRequestURL() :http://localhost/day14/demo1
URL:统一资源定位符 : http://localhost/day14/demo1 中华人民共和国
URI:统一资源标识符 : /day14/demo1 共和国 -
获取协议及版本:HTTP/1.1
String getProtocol() -
获取客户机的IP地址:
String getRemoteAddr()
代码演示:
@WebServlet("/dome04")
public class Servlet 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路径: /demo1
String servletPath = request.getServletPath();
System.out.println(servletPath);
//获取get的请求参数name=zhangsan
String queryString = request.getQueryString();
System.out.println(queryString);
//获取请求URL
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURI);
System.out.println(requestURL);
//获取协议或版本
String protocol = request.getProtocol();
System.out.println(protocol);
//获取客户机ip
String localAddr = request.getLocalAddr();
System.out.println(localAddr);
}
}
2.Request 获取请求头数据
方法:
String getHeader(String name):通过请求头的名称获取请求头的值
Enumeration getHeaderNames():获取所有的请求头名称
代码演示:
@WebServlet("/dome06")
public class Servlet1 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 s = headerNames.nextElement();
//根据名称请求
String headervalue = request.getHeader(s);
System.out.println(s+"---"+headervalue);
}
/* String header = request.getHeader("user-agent");
if (header.contains("chrome")){
System.out.println("chrome来了。。。。。");
}else if (header.contains("IE")) {
System.out.println("IE.。。。。。");
}*/
}
}
3. Request 获取请求体数据
请求体:只有POST请求方式,才有请求体
,在请求体中封装了POST请求的请求参数
步骤:
- 获取流对象
BufferedReader getReader():获取字符输入流,只能操作字符数据
ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据 - 再从流对象中拿数据
代码演示:
获取字符输入流
package Servlet;
@WebServlet("/dome09")
public class Servlet3 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 {
}
}
Request 反盗链案例
@WebServlet("/dome07")
public class Servlet2 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);//http://localhost:8080/hello.html
//反盗链
if (referer!=null){
if (referer.contains("/")) {
System.out.println("fandianying");
}else {
System.out.println("qu");
}
}
}
}
4.Request 其他功能:
获取请求参数通用方式:不论get还是post请求方式都可以使用下列方法来获取请求参数
- StringgetParameter(String name):根据参数名称获取参数值
username=zs&password=123
- String[] getParameterValues(String name):根据参数名称获取参数值的数组 hobby=xx&hobby=game
- Enumeration getParameterNames():获取所有请求的参数名称
- Map<String,String[]> getParameterMap():获取所有参数的map集合
Request 解决中文乱码问题:
get方式:tomcat 8 已经将get方式乱码问题解决了
post方式:会乱码
解决:在获取参数前,设置request的编码request.setCharacterEncoding("utf-8");
5.Request 请求转发
一种在服务器内部的资源跳转方式
- 通过request对象获取请求转发器对象:RequestDispatcher
getRequestDispatcher(String path)
- 使用RequestDispatcher对象来进行转发:
forward(ServletRequest request, ServletResponse response)
举例:
request.getRequestDispatcher("/SetServletDome1").forward(request,response);
转发特点:
1. 浏览器地址栏路径不发生变化
2. 只能转发到当前服务器内部资源中。
3. 转发是一次请求
6.Request 共享域(共享数据)
域对象:一个有作用范围的对象,可以在范围内共享数据
request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据
** 共享域方法:**
- void setAttribute(String name,Object obj):存储数据
- Object getAttitude(String name):通过键获取值
- void removeAttribute(String name):通过键移除键值对
四、ServletContext对象
1. 概念:
代表整个web应用,可以和程序的容器(服务器)来通信
2. 获取:
- 通过
request
对象获取
request.getServletContext(); - 通过
HttpServlet
获取
this.getServletContext(); 一般采用这种简单
实例
//request 获取ServletContext对象(第一种方式)
javax.servlet.ServletContext servletContext = request.getServletContext();
System.out.println(servletContext);
//HttpServlet 获取ServletContext对象(第二种方式)
javax.servlet.ServletContext servletContext1 = this.getServletContext();
System.out.println(servletContext1);
//判断地址是否是同一种
System.out.println(servletContext==servletContext1);//true
3. 功能:
1. ServletContext 获取MIME类型:
MIME
类型:在互联网通信过程中定义的一种文件数据类型
格式: 大类型/小类型 text/html image/jpeg
获取:==String getMimeType(String file)==
实例
@WebServlet("/ServletContext01")
public class ServletContext extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//HttpServlet 获取ServletContext对象
javax.servlet.ServletContext servletContext1 = this.getServletContext();
//获取MIME类型
String jpg = servletContext1.getMimeType("a.jpg");
System.out.println(jpg);//image/jpeg
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
2. ServletContext 域对象:共享数据
- setAttribute(String name,Object obj):存储数据
2.getAttitude(String name):通过键获取值 - removeAttribute(String name):通过键移除键值对
ServletContext对象范围:所有用户所有请求的数据
3. ServletContext 获取文件的真实(服务器)路径
- 方法:String getRealPath(String path)
String b = context.getRealPath("/b.txt");//web目录下资源访问
System.out.println(b);
String c = context.getRealPath("/WEB-INF/c.txt");//WEB-INF目录下的资源访问
System.out.println(c);
String a = context.getRealPath("/WEB-INF/classes/a.txt");//src目录下的资源访问
System.out.println(a);