Request對象
Request對象概念
在之前的service
方法中有兩個參數request
對象和response
對象, request
和response
對象是由服務器創建的。
在我們向服務器發出請求時請求肯定會攜帶請求參數,在Tomcat中創建了Request
對象,這個對象中封裝了請求消息數據;創建的Response
對象用來填充數據作爲響應,這兩個對象都將被作爲參數傳遞給service
方法
request
對象繼承體系結構:
ServletRequest – 接口
| 繼承
|
HttpServletRequest – 接口
| 實現
|
org.apache.catalina.connector.RequestFacade 類(tomcat)
Request對象的功能
-
獲取請求消息數據
-
獲取請求行數據
-
示例:GET http://localhost:8080/web/demo3 HTTP/1.1
-
方法:
-
獲取請求方式 :GET :
String getMethod()
-
獲取虛擬目錄:/web
String getContextPath()
-
獲取Servlet路徑:/demo3
String getServletPath()
-
獲取get方式請求參數:name=qiangjingzhou
String getQueryString()
-
獲取請求URI:/web/demo3
-
String getRequestURI()
: /web/demo3 -
StringBuffer getRequestURL()
: http://localhost:8080/web/demo3- URL:統一資源定位符 :http://localhost:8080/web/demo3 中華人民共和國
- URI:統一資源標識符 : /web/demo3 共和國
-
-
獲取協議及版本:HTTP/1.1
1.String getProtocol()
-
獲取客戶機的IP地址:
1.String getRemoteAddr()
-
-
-
獲取請求頭數據
方法:
-
String getHeader(String name)
:通過請求頭的名稱獲取請求頭的值 -
Enumeration<String> getHeaderNames()
:獲取所有的請求頭名稱
-
-
獲取請求體數據:
請求體:只有POST請求方式,纔有請求體,在請求體中封裝了POST請求的請求參數
步驟:
-
獲取流對象
BufferedReader getReader()
:獲取字符輸入流,只能操作字符數據ServletInputStream getInputStream()
:獲取字節輸入流,可以操作所有類型數據- 在文件上傳知識點後講解
-
再從流對象中拿數據
-
-
代碼演示
獲取請求行數據
在之前的項目中在web目錄下創建request目錄,在該目錄下創建RequestDemo1
類
該類的內容爲
package com.westos.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;
@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);
String servletPath = request.getServletPath();
System.out.println(servletPath);
String queryString = request.getQueryString();
System.out.println(queryString);
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURI);
System.out.println(requestURL);
String protocol = request.getProtocol();
System.out.println(protocol);
String remoteAddr = request.getRemoteAddr();
System.out.println(remoteAddr);
}
}
啓動項目,啓動後在瀏覽器訪問該項目,訪問URL爲,這裏我們偷了個懶,沒有通過表單提交數據,直接在url後添加了請求參數
http://localhost:8080/web/requestDemo1?name=qiangjingzhou
注意:在訪問前打開開發者工具
查看第一條請求
可以看到我們的請求數據,在IDEA 中查看控制檯的輸出
可以看到在控制檯輸出了請求頭中的數據,可以通過這些數據做出相應的響應
獲取的IP是IPV6版本的IP地址
這裏需要注意一點,瀏覽器提交請求的方式默認是GET方式,當請求被提交到Servlet後,Servlet會根據請求方式是GET還是POST來執行相應的doGet還是doPost方法,我們需要將上面獲得請求頭數據的代碼寫到doGet方法中,如果寫到doPost方法中就獲取不到
獲取請求頭數據
在request目錄下新建RequestDemo2,注意修改映射名
String getHeader(String name)
:通過請求頭的名稱獲取請求頭的值
Enumeration<String> getHeaderNames()
:獲取所有的請求頭名稱
RequestDemo2中的內容爲
package com.westos.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;
import java.util.Enumeration;
@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);
}
}
}
運行項目,在瀏覽器訪問localhost:8080/web/requestDemo2
可以看到控制檯輸出的請求頭是以鍵值對的形式存在
但是Enumeration<String> getHeaderNames()
方法用的比較少,因爲我們一般在獲取請求頭的值的時候一般是知道請求頭的名稱的
可以獲取user-agent
的內容,判斷向服務器提交請求的瀏覽器的版本以及瀏覽器的種類來設置不同的響應來解決瀏覽器兼容的問題
對之前的項目做一些修改
package com.westos.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;
import java.util.Enumeration;
@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);
}
String header = request.getHeader("user-agent");
if (header.contains("Firefox")){
System.out.println("火狐來了");
}else if (header.contains("Chrome")){
System.out.println("谷歌來了");
}
}
}
運行項目
在火狐瀏覽器上訪問http://localhost:8080/web/requestDemo2
在谷歌瀏覽器上訪問同樣的url
前面的輸出結果是因爲我沒有刪除之前的獲取請求頭的代碼,通過這樣的方式可以解決瀏覽器兼容的問題
獲取請求體數據
只有POST請求的方式纔會有請求體,在請求體中封裝的POST的請求參數
步驟:
-
獲取流對象
BufferedReader getReader()
:獲取字符輸入流,只能操作字符數據ServletInputStream getInputStream()
:獲取字節輸入流,可以操作所有類型數據- 在文件上傳知識點後講解
-
再從流對象中拿數據
在web目錄下創建register.html文檔,文檔的內容爲
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/web/requestDemo3" method="post">
<input type="text" placeholder="請輸入用戶名", name="username">
<br>
<input type="text" placeholder="請輸入密碼", name="password">
<br>
<input type="submit" value="登錄">
</form>
</body>
</html>
在request目錄下創建 RequestDemo3
package com.westos.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;
import java.util.Enumeration;
@WebServlet("/requestDemo3")
public class RequestDemo3 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 {
}
}
運行項目,在瀏覽器訪問URL http://localhost:8080/web/register.html
輸入用戶名和密碼
點擊登錄
可以在終端看到我們的請求參數
獲取請求參數的通用方式
在request
中存在一些方法
獲取請求參數通用方式:不論get還是post請求方式都可以使用下列方法來獲取請求參數
String getParameter(String name)
:根據參數名稱獲取參數值 username=qiangjingzhou&password=12138String[] getParameterValues(String name)
:根據參數名稱獲取參數值的數組 (多用於複選框)
hobby=xx&hobby=gameEnumeration<String> getParameterNames()
:獲取所有請求的參數名稱Map<String,String[]> getParameterMap()
:獲取所有參數的map集合
在web目錄下創建register1.html文檔
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/web/requestDemo4" method="post">
<input type="text" placeholder="請輸入用戶名", name="username">
<br>
<input type="text" placeholder="請輸入密碼", name="password">
<br>
<input type="submit" value="登錄">
</form>
</body>
</html>
在request目錄下創建RequestDemo4
package com.westos.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;
@WebServlet("/requestDemo4")
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
System.out.println("POST");
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
System.out.println("GET");
System.out.println(username);
}
}
在Servlet中獲取請求參數username
的值,因爲獲取請求參數的方法在POST和GET是通用的,所以兩種方法除了System.out.println("GET");
和System.out.println("POST");
的結果不同,其餘如果在請求參數一致只有提交請求方式不同的情況下我們獲取的參數的值是一樣的
運行項目,在瀏覽器訪問http://localhost:8080/web/register1.html
訪問完成後,輸入用戶名和密碼
可以看到請求的提交方式以及請求參數username的值
修改文檔中的表單提交方式,運行項目,提交表單
可以看到提交方式發生改變,但是獲取的參數和之前使用POST提交方式獲取的參數一致,既然獲取到的值是一致的並且代碼也是一致通用的,就可以在doPost
方法中調用doGet
方法或者在doGet
方法中調用 doPost
方法
package com.westos.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;
@WebServlet("/requestDemo4")
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
這樣可以節省許多代碼,後續我們都會這樣使用
接下來演示String[] getParameterValues(String name)
方法,這個方法主要應用於複選框,因爲在複選框中一個名稱對應多個請求參數的值
在web目錄下創建register2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/web/requestDemo4" method="get">
<input type="text" placeholder="請輸入用戶名", name="username">
<br>
<input type="text" placeholder="請輸入密碼", name="password">
<br>
<input type="checkbox" name="hobby" value="game">遊戲
<input type="checkbox" name="hobby" value="study">學習
<br>
<input type="submit" value="登錄">
</form>
</body>
</html>
在request目錄下創建RequestDemo5
package com.westos.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;
@WebServlet("/requestDemo5")
public class RequestDemo5 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);
}
}
運行項目,在瀏覽器訪問localhost:8080/web/register2.html
點擊登錄,可以看到控制檯的輸出
在上面的代碼中添加
Enumeration<String> parameterNames = request.getParameterNames();
while(parameterNames.hasMoreElements()){
String s = parameterNames.nextElement();
System.out.println(s);
}
啓動項目,在瀏覽器訪問,隨便輸入一些參數,提交表單
獲取到了請求參數的名稱