Java前端開發(fā)之HttpServletRequest的使用
接口詳解
搭設(shè)基本測試環(huán)境
web 下新建 reg.html 文件,用作注冊(cè)網(wǎng)頁;
這里使用了 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è)頁面設(shè)置為歡迎頁
<welcome-file-list>
<welcome-file>reg.html</welcome-file>
</welcome-file-list>
新建測試 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ì)的形式存在;
后端我們僅需重寫 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ù)組;
注意:無論你在前端傳入的是什么樣的數(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 傳遞過來的 request,那到時(shí)間并輸出
首先我們處理 BServlet 的代碼:
使用 getAttribute 方法獲取到 request 中存儲(chǔ)的參數(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:
想要把當(dāng)前 Servlet 中的 request 傳遞給下一個(gè) Servlet 請(qǐng)按兩步走:
- 獲取下一 Servlet 請(qǐng)求轉(zhuǎn)發(fā)器對(duì)象 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();
// 按照鍵值對(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ā)過程濃縮為一行代碼:
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 常見方法
設(shè)置字符集
在 tomcat10 之前,默認(rèn)字符集并非 UTF-8,直接使用 GET 或者 POST 獲取到的數(shù)據(jù)都是亂碼,所以需要手動(dòng)設(shè)置;
修改請(qǐng)求 request 亂碼問題:
req.setCharacterEncoding("UTF-8");
修改響應(yīng) response 亂碼問題:
resp.setContentType("text/html;charset=UTF-8");
幾種常見屬性獲取方式
// 動(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前端開發(fā)之HttpServletRequest的使用的文章就介紹到這了,更多相關(guān)Java HttpServletRequest內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在springboot中注入FilterRegistrationBean不生效的原因
這篇文章主要介紹了在springboot中注入FilterRegistrationBean不生效的原因及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring Boot攔截器Interceptor與過濾器Filter詳細(xì)教程(示例詳解)
本文詳細(xì)介紹了SpringBoot中的攔截器(Interceptor)和過濾器(Filter),包括它們的定義、作用范圍、使用場景、實(shí)現(xiàn)步驟、執(zhí)行順序、常見問題及解決方案,感興趣的朋友跟隨小編一起看看吧2025-03-03
idea中定時(shí)及多數(shù)據(jù)源配置方法
因項(xiàng)目要求,需要定時(shí)從達(dá)夢(mèng)數(shù)據(jù)庫中取數(shù)據(jù),并插入或更新到ORACLE數(shù)據(jù)庫中,這篇文章主要介紹了idea中定時(shí)及多數(shù)據(jù)源配置方法,需要的朋友可以參考下2023-12-12
Java/Android 獲取網(wǎng)絡(luò)重定向文件的真實(shí)URL的示例代碼
本篇文章主要介紹了Java/Android 獲取網(wǎng)絡(luò)重定向文件的真實(shí)URL的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

