Java前端開(kāi)發(fā)之HttpServletRequest的使用
接口詳解
搭設(shè)基本測(cè)試環(huán)境
web 下新建 reg.html
文件,用作注冊(cè)網(wǎng)頁(yè);
這里使用了 form 表單,注意提交的 action 是 根目錄 + servlet的url;
請(qǐng)求方式我們使用 post
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> </head> <body> <h1>用戶注冊(cè)</h1> <form action="/05/rds" method="post"> username: <input type="text" name="username" /><br /> password: <input type="password" name="password" /><br /> <input type="submit" value="reg" /> </form> </body> </html>
在 web.xml 中把注冊(cè)頁(yè)面設(shè)置為歡迎頁(yè)
<welcome-file-list> <welcome-file>reg.html</welcome-file> </welcome-file-list>
新建測(cè)試 servlet,然后記得在 web.xml 中注冊(cè)
package com.zhiyiyi.javaweb.servlet; ... // 依舊使用HttpServlet接口 public class RequestDemoServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" version="5.0"> <!-- 注冊(cè)servlet --> <servlet> <servlet-name>requestDemoServlet</servlet-name> <servlet-class>com.zhiyiyi.javaweb.servlet.RequestDemoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>requestDemoServlet</servlet-name> <url-pattern>/rds</url-pattern> </servlet-mapping> ... </web-app>
取回 keys
因?yàn)槲覀冊(cè)?reg.html 中使用 post 請(qǐng)求后端,攜帶的參數(shù)將以鍵值對(duì)的形式存在;
后端我們僅需重寫(xiě) doPost 方法即可對(duì)前端請(qǐng)求作出響應(yīng);
代碼內(nèi)容和之前所學(xué)的一致,使用 getParameterNames
獲取所有參數(shù)的 keys;
之后遍歷以下輸出所有 keys
public class RequestDemoServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Enumeration<String> names = req.getParameterNames(); while (names.hasMoreElements()) { String name = names.nextElement(); System.out.println(name); } } }
取回 values
因?yàn)榻^大多數(shù)情況下,我們均會(huì)知曉前端傳入?yún)?shù)的 keys,即可以直接使用 key 獲取對(duì)應(yīng)的 value;
getParameter 如果一個(gè) key 僅對(duì)應(yīng)一個(gè) value,使用此方法;
getParameterValues 若一個(gè) key 對(duì)應(yīng)多個(gè) values,使用此方法返回一個(gè)字符串?dāng)?shù)組;
注意:無(wú)論你在前端傳入的是什么樣的數(shù)據(jù)類型,在后端所有的 keys 和 values 均為字符串形式!
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); String[] password = req.getParameterValues("password"); System.out.println(username); System.out.println(Arrays.toString(password)); }
請(qǐng)求域與應(yīng)用域
應(yīng)用域?qū)ο?/h3>
- servletcontext 對(duì)象
- 緩存技術(shù),如常量池、線程池、鏈接池
請(qǐng)求域?qū)ο?/h3>
請(qǐng)求域的生命周期很短,作用范圍僅一次請(qǐng)求;
請(qǐng)求結(jié)束后,請(qǐng)求域就會(huì)銷毀;
請(qǐng)盡量控制對(duì)象的大小,以便適配請(qǐng)求域和應(yīng)用域;
跳轉(zhuǎn)與轉(zhuǎn)發(fā)
轉(zhuǎn)發(fā)一次請(qǐng)求
我們目前要實(shí)現(xiàn)的效果:
- AServlet 把當(dāng)前時(shí)間封裝到 request 內(nèi)并發(fā)送給 BServlet
- BServlet 獲取 AServlet 傳遞過(guò)來(lái)的 request,那到時(shí)間并輸出
首先我們處理 BServlet 的代碼:
使用 getAttribute
方法獲取到 request 中存儲(chǔ)的參數(shù)
public class BServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 獲取傳遞過(guò)來(lái)的參數(shù) Object time = req.getAttribute("systime"); // 打印出來(lái) resp.setContentType("text/html"); PrintWriter writer = resp.getWriter(); writer.println(time); } }
之后處理 AServlet:
想要把當(dāng)前 Servlet 中的 request 傳遞給下一個(gè) Servlet 請(qǐng)按兩步走:
- 獲取下一 Servlet 請(qǐng)求轉(zhuǎn)發(fā)器對(duì)象 RequestDispatcher(getRequestDispatcher 中的參數(shù)填寫(xiě)下一 Servlet 的 url)
- 調(diào)用 RequestDispatcher 的 forward 方法將 request 傳遞下去
public class AServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Date time = new Date(); // 按照鍵值對(duì)的方式設(shè)置存儲(chǔ)到request中的參數(shù)值 req.setAttribute("systime", time); // 第一步:獲取請(qǐng)求轉(zhuǎn)發(fā)器對(duì)象RequestDispatcher RequestDispatcher requestDispatcher = req.getRequestDispatcher("/bs"); // 第二步:調(diào)用forward方法將request傳遞下去 requestDispatcher.forward(req, resp); } }
一般我們會(huì)把轉(zhuǎn)發(fā)過(guò)程濃縮為一行代碼:
req.getRequestDispatcher("/bs").forward(req, resp);
事實(shí)上,你可以吧 AServlet 理解為一個(gè)中間件,而 forward 方法可以等同于 express.js 中的 next 方法
轉(zhuǎn)發(fā)要求
轉(zhuǎn)發(fā)目標(biāo)不一定是 servlet,他可以是任意一個(gè) tomcat 所承認(rèn)的資源(譬如 html);
但請(qǐng)注意轉(zhuǎn)發(fā)路徑不可以包含項(xiàng)目名稱!
譬如我在 web 下新建 login.html ,那么轉(zhuǎn)發(fā)路徑就是 /login.html
轉(zhuǎn)發(fā)區(qū)別
getParameter 方法,獲取的是由瀏覽器提交的表單的數(shù)據(jù);
getAttribute 方法,獲取的是請(qǐng)求域中綁定的數(shù)據(jù);
request 常見(jiàn)方法
設(shè)置字符集
在 tomcat10 之前,默認(rèn)字符集并非 UTF-8,直接使用 GET 或者 POST 獲取到的數(shù)據(jù)都是亂碼,所以需要手動(dòng)設(shè)置;
修改請(qǐng)求 request 亂碼問(wèn)題:
req.setCharacterEncoding("UTF-8");
修改響應(yīng) response 亂碼問(wèn)題:
resp.setContentType("text/html;charset=UTF-8");
幾種常見(jiàn)屬性獲取方式
// 動(dòng)態(tài)獲取應(yīng)用根路徑 String contextPath = req.getContextPath(); // 獲取請(qǐng)求方式 String method = req.getMethod(); // 獲取請(qǐng)求的URI String requestURI = req.getRequestURI(); // 獲取servlet路徑 String servletPath = req.getServletPath();
到此這篇關(guān)于Java前端開(kāi)發(fā)之HttpServletRequest的使用的文章就介紹到這了,更多相關(guān)Java HttpServletRequest內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)從方法返回多個(gè)值功能示例
這篇文章主要介紹了java實(shí)現(xiàn)從方法返回多個(gè)值功能,結(jié)合實(shí)例形式分析了集合類、封裝對(duì)象、引用傳遞三種實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10java根據(jù)方法名稱取得反射方法的參數(shù)類型示例
利用java反射原理調(diào)用方法時(shí),常先需要傳入方法參數(shù)數(shù)組才能取得方法。該方法參數(shù)數(shù)組采用動(dòng)態(tài)取得的方式比較合適2014-02-02通過(guò)jenkins發(fā)布java項(xiàng)目到目標(biāo)主機(jī)上的詳細(xì)步驟
這篇文章主要介紹了通過(guò)jenkins發(fā)布java項(xiàng)目到目標(biāo)主機(jī)上的詳細(xì)步驟,發(fā)布java項(xiàng)目的步驟很簡(jiǎn)單,通過(guò)拉取代碼并打包,備份目標(biāo)服務(wù)器上已有的要發(fā)布項(xiàng)目,具體內(nèi)容詳情跟隨小編一起看看吧2021-10-10maven子模塊相互依賴打包時(shí)報(bào)錯(cuò)找不到類的解決方案
本文主要介紹了maven子模塊相互依賴打包時(shí)報(bào)錯(cuò)找不到類的解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06spring security自定義認(rèn)證登錄的全過(guò)程記錄
這篇文章主要給大家介紹了關(guān)于spring security自定義認(rèn)證登錄的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12mybatis plus saveBatch方法方法執(zhí)行慢導(dǎo)致接口發(fā)送慢解決分析
這篇文章主要為大家介紹了mybatis plus saveBatch方法導(dǎo)致接口發(fā)送慢解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10