Java前端開發(fā)之HttpServletRequest的使用
接口詳解
搭設基本測試環(huán)境
web 下新建 reg.html 文件,用作注冊網(wǎng)頁;
這里使用了 form 表單,注意提交的 action 是 根目錄 + servlet的url;
請求方式我們使用 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>用戶注冊</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 中把注冊頁面設置為歡迎頁
<welcome-file-list>
<welcome-file>reg.html</welcome-file>
</welcome-file-list>
新建測試 servlet,然后記得在 web.xml 中注冊
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">
<!-- 注冊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
因為我們在 reg.html 中使用 post 請求后端,攜帶的參數(shù)將以鍵值對的形式存在;
后端我們僅需重寫 doPost 方法即可對前端請求作出響應;
代碼內(nèi)容和之前所學的一致,使用 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
因為絕大多數(shù)情況下,我們均會知曉前端傳入?yún)?shù)的 keys,即可以直接使用 key 獲取對應的 value;
getParameter 如果一個 key 僅對應一個 value,使用此方法;
getParameterValues 若一個 key 對應多個 values,使用此方法返回一個字符串數(shù)組;
注意:無論你在前端傳入的是什么樣的數(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ū)ο?/h3>
- servletcontext 對象
- 緩存技術,如常量池、線程池、鏈接池
請求域?qū)ο?/h3>
請求域的生命周期很短,作用范圍僅一次請求;
請求結(jié)束后,請求域就會銷毀;
請盡量控制對象的大小,以便適配請求域和應用域;
跳轉(zhuǎn)與轉(zhuǎn)發(fā)
轉(zhuǎn)發(fā)一次請求
我們目前要實現(xiàn)的效果:
- AServlet 把當前時間封裝到 request 內(nèi)并發(fā)送給 BServlet
- BServlet 獲取 AServlet 傳遞過來的 request,那到時間并輸出
首先我們處理 BServlet 的代碼:
使用 getAttribute 方法獲取到 request 中存儲的參數(shù)
public class BServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 獲取傳遞過來的參數(shù)
Object time = req.getAttribute("systime");
// 打印出來
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
writer.println(time);
}
}
之后處理 AServlet:
想要把當前 Servlet 中的 request 傳遞給下一個 Servlet 請按兩步走:
- 獲取下一 Servlet 請求轉(zhuǎn)發(fā)器對象 RequestDispatcher(getRequestDispatcher 中的參數(shù)填寫下一 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();
// 按照鍵值對的方式設置存儲到request中的參數(shù)值
req.setAttribute("systime", time);
// 第一步:獲取請求轉(zhuǎn)發(fā)器對象RequestDispatcher
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/bs");
// 第二步:調(diào)用forward方法將request傳遞下去
requestDispatcher.forward(req, resp);
}
}
一般我們會把轉(zhuǎn)發(fā)過程濃縮為一行代碼:
req.getRequestDispatcher("/bs").forward(req, resp);
事實上,你可以吧 AServlet 理解為一個中間件,而 forward 方法可以等同于 express.js 中的 next 方法
轉(zhuǎn)發(fā)要求
轉(zhuǎn)發(fā)目標不一定是 servlet,他可以是任意一個 tomcat 所承認的資源(譬如 html);
但請注意轉(zhuǎn)發(fā)路徑不可以包含項目名稱!
譬如我在 web 下新建 login.html ,那么轉(zhuǎn)發(fā)路徑就是 /login.html
轉(zhuǎn)發(fā)區(qū)別
getParameter 方法,獲取的是由瀏覽器提交的表單的數(shù)據(jù);
getAttribute 方法,獲取的是請求域中綁定的數(shù)據(jù);
request 常見方法
設置字符集
在 tomcat10 之前,默認字符集并非 UTF-8,直接使用 GET 或者 POST 獲取到的數(shù)據(jù)都是亂碼,所以需要手動設置;
修改請求 request 亂碼問題:
req.setCharacterEncoding("UTF-8");
修改響應 response 亂碼問題:
resp.setContentType("text/html;charset=UTF-8");
幾種常見屬性獲取方式
// 動態(tài)獲取應用根路徑 String contextPath = req.getContextPath(); // 獲取請求方式 String method = req.getMethod(); // 獲取請求的URI String requestURI = req.getRequestURI(); // 獲取servlet路徑 String servletPath = req.getServletPath();
到此這篇關于Java前端開發(fā)之HttpServletRequest的使用的文章就介紹到這了,更多相關Java HttpServletRequest內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java根據(jù)方法名稱取得反射方法的參數(shù)類型示例
利用java反射原理調(diào)用方法時,常先需要傳入方法參數(shù)數(shù)組才能取得方法。該方法參數(shù)數(shù)組采用動態(tài)取得的方式比較合適2014-02-02
通過jenkins發(fā)布java項目到目標主機上的詳細步驟
這篇文章主要介紹了通過jenkins發(fā)布java項目到目標主機上的詳細步驟,發(fā)布java項目的步驟很簡單,通過拉取代碼并打包,備份目標服務器上已有的要發(fā)布項目,具體內(nèi)容詳情跟隨小編一起看看吧2021-10-10
mybatis plus saveBatch方法方法執(zhí)行慢導致接口發(fā)送慢解決分析
這篇文章主要為大家介紹了mybatis plus saveBatch方法導致接口發(fā)送慢解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10

