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);