Servlet技術12_HttpServletRequest類
HttpServletRequest 類有什麼作用:
每次只要有請求進入 Tomcat 伺服器,Tomcat 伺服器就會把請求過來的 HTTP 協議資訊解析好封裝到 Request 物件中。 然後傳遞到 service 方法(doGet 和 doPost)中給我們使用。我們可以通過 HttpServletRequest 物件,獲取到所有請求的 資訊。
HttpServletRequest 類的常用方法:
i. getRequestURI() 獲取請求的資源路徑
ii. getRequestURL() 獲取請求的統一資源定位符(絕對路徑)
iii. getRemoteHost() 獲取客戶端的 ip 地址
iv. getHeader() 獲取請求頭
v. getParameter() 獲取請求的引數
vi. getParameterValues() 獲取請求的引數(多個值的時候使用)
vii. getMethod() 獲取請求的方式 GET 或 POST
viii. setAttribute(key, value); 設定域資料
ix. getAttribute(key); 獲取域資料
x. getRequestDispatcher() 獲取請求轉發物件
常用 API 示例程式碼:
package com.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RequestAPIServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// i. getRequestURI() 獲取請求的資源路徑
System.out.println("URI ==> " + req.getRequestURI());
// ii. getRequestURL() 獲取請求的統一資源定位符(絕對路徑)
System.out.println("URL ==> " + req.getRequestURL());
/* 正常情況下,idea中
使用localhost訪問時,得到的客戶端ip地址是 ===> 127.0.0.1
使用127.0.0.1訪問時,得到的客戶端ip地址是 ===> 127.0.0.1
使用真實的電腦ip訪問時,得到的客戶端ip地址是 ===> 真實的客戶端ip地址
*/
// iii. getRemoteHost() 獲取客戶端的 ip 地址
System.out.println("客戶端ip ==> " + req.getRemoteHost());
// iv. getHeader() 獲取請求頭
System.out.println("請求頭User-Agent ===> " + req.getHeader("User-Agent"));
// vii. getMethod() 獲取請求的方式 GET 或 POST
System.out.println("請求的方式:" + req.getMethod());
}
}
執行結果:
如何獲取請求引數:
建立一個form.html
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8080/07_servlet/parameterServlet" method="get">
使用者名稱:<input type="text" name="username" ><br/>
密碼:<input type="password" name="password" ><br/>
興趣愛好:<input type="checkbox" name="hobby" value="cpp">C++
<input type="checkbox" name="hobby" value="java">java
<input type="checkbox" name="hobby" value="js">JavaScript
<input type="submit">
</form>
</body>
</html>
建立一個ParameterServlet.java
package com.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
public class ParameterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//獲取請求引數
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobby = req.getParameterValues("hobby");
System.out.println("使用者名稱:" + username);
System.out.println("密碼:" + password);
System.out.println("興趣愛好:" + Arrays.asList(hobby));
}
}
當然,別忘了配置xml。
執行結果:
解決post請求中文亂碼問題:
在使用者名稱框內輸入中文,get請求正常,post請求出現亂碼問題:
用一個api就可以了(在獲取引數請求之前才有用)
//設定請求體的字符集為UTF-8,從而解決post請求的中文亂碼問題
req.setCharacterEncoding("UTF-8");
這樣,問題就可以得到解決
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//設定請求體的字符集為UTF-8,從而解決post請求的中文亂碼問題
req.setCharacterEncoding("UTF-8");
System.out.println("---------doPost-----------");
//獲取請求引數
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobby = req.getParameterValues("hobby");
System.out.println("使用者名稱:" + username);
System.out.println("密碼:" + password);
System.out.println("興趣愛好:" + Arrays.asList(hobby));
}
請求的轉發:
什麼是請求轉發?
請求轉發是指,伺服器收到請求後,從一次資源跳轉到另一個資源的操作叫請求轉發。
Servlet1:
package com.servlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//獲取請求引數(辦事的材料)
String username = req.getParameter("username");
System.out.println("在Servlet1(櫃檯1)中檢視的引數(材料):" + username);
//給材料蓋一個章,並傳遞到Servlet2(櫃檯2)檢視
req.setAttribute("key1","櫃檯1的章");
//問路:Servlet2(櫃檯2)怎麼走
/**
* 請求轉發必須要以 / 打頭,斜槓表示地址為: http://ip:port/工程名/ ,對映到 idea 程式碼的 web 目錄
*/
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");
//走向Servlet2
requestDispatcher.forward(req,resp);
}
}
Servlet2:
package com.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Servlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//獲取請求引數(辦事的材料)
String username = req.getParameter("username");
System.out.println("在Servlet2(櫃檯2)中檢視的引數(材料):" + username);
//檢視櫃檯1改的章
Object key1 = req.getAttribute("key1");
System.out.println("櫃檯1是否有章:" + key1);
//處理自己的業務
System.out.println("Servlet2處理自己的業務");
}
}
請求轉發的特點:
- 瀏覽器位址列沒有變化
- 他們是一次請求
- 他們共享Request域中的資料
- 可以轉發到WEB-INF目錄下(form.html不可以部署到WEB-INF目錄下,但是可以通過請求轉發訪問進來)
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/WEB-INF/form.html");
base 標籤的作用:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首頁</title>
</head>
<body>
這是web下的index.html<br/>
<a href="a/b/c.html">a/b/c.html</a>
<br/>
<a href="http://localhost:8080/07_servlet/forwardC">請求轉發:a/b/c.html</a>
</body>
</html>
c.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首頁</title>
</head>
<body>
這是web/a/b下的c.html<br/>
<a href="../../index.html">跳回首頁</a>
</body>
</html>
ForwardC.java
public class ForwardC extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("經過了ForwardC程式");
req.getRequestDispatcher("/a/b/c.html").forward(req,resp);
}
}
當我們點選index.html中的a標籤進行跳轉的時候,瀏覽器的位址列中的地址是:
http://localhost:8080/07_servlet/a/b/c.html
跳轉回去的a標籤路徑是:
../../index.html
所有相對路徑在工作的時候都會參照當前瀏覽器位址列中的地址來進行跳轉
那麼參照後得到的地址是:
http://localhost:8080/07_servlet/index.html
正確的跳轉路徑
當我們使用請求轉發進行跳轉的時候,瀏覽器位址列中的地址是:
http://localhost:8080/07_servlet/forwardC
跳轉回去的a標籤路徑是:
../../index.html
所有相對路徑在工作的時候都會參照當前瀏覽器位址列中的地址來進行跳轉。
那麼參照後得到的地址是:
http://localhost:8080/index.html
這是錯誤的路徑!
base標籤可以設定當前頁面中所有相對路徑工作時,參照哪個路徑進行跳轉。
修改後的程式碼:
c.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首頁</title>
<!-- base標籤設定頁面相對路徑工作時參照的地址
href屬性就是引數的地址值-->
<base href="http://localhost:8080/07_servlet/a/b/c.html">
</head>
<body>
這是web/a/b下的c.html<br/>
<a href="../../index.html">跳回首頁</a>
</body>
</html>
說明:
- 首先,會檢視有無base標籤,要是有base標籤,他就會忽略掉瀏覽器當前位址列中的地址,而是參考base的值。相當於把地址放到
../../index.html
前面去了:
http://localhost:8080/07_servlet/a/b/c.html../../index.html
一個../
抵消一個資料夾,也就是等於
http://localhost:8080/07_servlet/index.html
- base標籤中最後的資源名(c.html)是可以省略的(記住: / 不能省略!)
<base href="http://localhost:8080/07_servlet/a/b/">
Web中的相對路徑和絕對路徑:
在 JavaWeb 中,路徑分為相對路徑和絕對路徑兩種:
相對路徑是:
. 表示當前目錄
… 表示上一級目錄
資源名 表示當前目錄/資源名
絕對路徑:
http://ip:port/工程路徑/資源路徑
在實際開發中,路徑都使用絕對路徑,而不簡單的使用相對路徑。
1、絕對路徑
2、base+相對路徑
web 中 / 斜槓的不同意義:
在 web 中 / 斜槓 是一種絕對路徑。
/ 斜槓 如果被瀏覽器解析,得到的地址是:http://ip:port/ http://localhost:8080/
<a href="/">斜槓</a>
/ 斜槓 如果被伺服器解析,得到的地址是:http://ip:port/工程路徑 http://localhost:8080/07_servlet/
<url-pattern>/servlet1</url-pattern>
servletContext.getRealPath(“/”);
request.getRequestDispatcher(“/”);
特殊情況: response.sendRediect(“/”); 把斜槓傳送給瀏覽器解析。得到 http://ip:port/
相關文章
- Servlet技術---Servlet深化Servlet
- Servlet技術----Servlet入門Servlet
- Java中Servlet技術JavaServlet
- Servlet技術入門Servlet
- 【菜鳥學Java】4:Servlet 技術JavaServlet
- Servlet開發技術,建立,以及Servlet的配置,web.xml的配置ServletWebXML
- 批判性地看待一種可行的表示技術:JavaServerPages servlet技術(上) (轉)JavaServerServlet
- 走進JavaWeb技術世界4:Servlet 工作原理詳解JavaWebServlet
- 受益技術類書籍
- 第2,8章 servlet 開發技術(12課時)Servlet
- 技術類專業和思想類專業
- iOS面試題一(技術類)iOS面試題
- 資料倉儲技術分類術語
- servlet的過濾器filter類Servlet過濾器Filter
- 如何閱讀技術類書籍
- Java技術 —— 類的設計技巧Java
- 技術類視訊教程網站網站
- 如果IT技術按照兵種分類
- 物聯網6類技術無線連線技術的分析
- 關於Servlet的原理以及常用類Servlet
- Java 技術之類載入機制Java
- framework外掛化技術-類載入Framework
- 技術類書籍中的形容詞
- 佩羅的技術分類(轉載)
- 汪子熙的SAP技術文章分類合集
- 技術人必看的各類工具書籍
- OVS+DPDK Datapath 包分類技術
- 物件導向的類測試技術研究物件
- UI 類面試題彙總 | 掘金技術徵文UI面試題
- 我的另類秋招 | 掘金技術徵文
- 例說 WIN SDK 視窗子類化技術
- VB中子分類技術的應用 (轉)
- 用javac編譯servlet類出現問題Java編譯Servlet
- Servlet第五篇【介紹會話技術、Cookie的API、詳解、應用】Servlet會話CookieAPI
- 費埃哲:分析技術具備類人類思維–資訊圖
- Mybatis技術內幕(2.3.5):反射模組-Property工具類MyBatis反射
- GIFTO背後區塊鏈技術的分類區塊鏈
- 技術問答集錦(11)JVM類載入JVM