request 對象是 httpservletrequestwrapper 類的實(shí)例。它的繼承體系如下:
_request 對象繼承層次結(jié)構(gòu)圖.png
servletrequest 接口的唯一子接口是 httpservletrequest ,httpservletrequest 接口的唯一實(shí)現(xiàn)類 httpservletrequestwrapper ,單從 request 對象一脈單傳的類繼承體系可以看出,javaweb 標(biāo)準(zhǔn)類庫只支持了 http 協(xié)議。 servlet/jsp 中大量使用了接口而不是實(shí)現(xiàn)類,這恰恰就是面向接口編程的最佳應(yīng)用啊。
request 內(nèi)置對象是由 tomcat 創(chuàng)建的,可以用來封裝 http 請求參數(shù)信息、進(jìn)行屬性值的傳遞以及完成服務(wù)端跳轉(zhuǎn),這就是 request 對象最重要的三個功能了。
request 對象的創(chuàng)建流程
一旦 http 請求報(bào)文發(fā)送到 tomcat 中, tomcat 對數(shù)據(jù)進(jìn)行解析,就會立即創(chuàng)建 request 對象,并對參數(shù)賦值,然后將其傳遞給對應(yīng)的 jsp/servlet 。一旦請求結(jié)束,request 對象就會立即被銷毀。服務(wù)端跳轉(zhuǎn),因?yàn)槿匀皇峭淮握埱?,所以這些頁面會共享一個 request 對象。
1、訪問請求參數(shù)
傳遞參數(shù)
login.jsp關(guān)鍵 代碼
<%= "name:"+new string(request.getparameter("name").getbytes("iso-8859-1"),"utf-8") %> <%= "sex:"+request.getparameter("sex") %> <%= "id:"+request.getparameter("id") %> <%= "pwd:"+request.getparameter("pwd") %>
說明:如果指定的參數(shù)不存在,將返回null;如果指定了參數(shù)名,但未指定參數(shù)值,將返回空的字符串"。
因?yàn)樗械膔equest請求都是iso-8859-1的,而在頁面采用的是utf-8編碼方式,所以在遇到中文時,將獲取到的數(shù)據(jù)通過string的構(gòu)造方法使用指定的編碼類型重新構(gòu)造一個string對象。
2、在作用域中管理屬性
在進(jìn)行請求轉(zhuǎn)發(fā)時,需要把一些數(shù)據(jù)傳遞到轉(zhuǎn)發(fā)后的頁面進(jìn)行處理,這時,需要用request對象的setattribute方法將數(shù)據(jù)保存在request范圍內(nèi)的變量中。
<% try { int money = 100; int number = 0; request.setattribute("result", money / number); } catch (exception e) { request.setattribute("result", "很抱歉,頁面產(chǎn)生錯誤!"); } %>
<%= request.getattribute("result").tostring() %>
由于getattribute方法返回值是object,需要調(diào)用tostring方法轉(zhuǎn)換為字符串。
3、獲取cookie
cookie是小段文本信息,在網(wǎng)絡(luò)服務(wù)器上生成,并發(fā)送給瀏覽器。通過cookie可以標(biāo)識用戶身份,記錄用戶名和密碼,跟蹤重復(fù)用戶等。以鍵值對形式保存在客戶機(jī)的某個目錄下。
<% cookie[] cookies = request.getcookies(); string user = ""; string date = ""; if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies[i].getname().equals("mrcookie")) { user = urldecoder.decode(cookies[i].getvalue().split("#")[0]); date = cookies[i].getvalue().split("#")[1]; } } } if ("".equals(user) && "".equals(date)) { %> 游客您好,歡迎您初次光臨! 請輸入姓名: <% } else { %> 歡迎 <%=user%> 再次光臨 <% }%>
deal.jsp:
<% request.setcharacterencoding("gb18030"); string user = urlencoder.encode(request.getparameter("user"), "utf-8"); cookie cookie = new cookie("mrcookie", user + "#" + new date().tolocalestring()); cookie.setmaxage(60 * 60 * 24 * 30); response.addcookie(cookie); %> window.location.href = "index.jsp"
4、獲取客戶端信息
客戶提交信息的方式:<%=request.getmethod() %>
使用的協(xié)議:<%=request.getprotocol() %>
客戶端地址:<%=request.getrequesturl() %>
客戶端ip地址:<%=request.getremoteaddr() %>
服務(wù)器端口號:<%=request.getserverport() %>
服務(wù)器名稱:<%=request.getservername() %>
客戶端主機(jī)名:<%=request.getremotehost() %>
客戶端所請求的腳本文件的文件路徑:<%=request.getservletpath() %>
http協(xié)議定義的文件頭信息host的值:<%=request.getheader("host") %>
http協(xié)議定義的文件頭信息user-agent的值:<%=request.getheader("user-agent") %>
http協(xié)議定義的文件頭信息accept-language的值:<%=request.getheader("accept-language") %>
請求文件的絕對路徑:<%=request.getrealpath("index.jsp") %>
5、顯示國際化信息
瀏覽器可以通過accept-language的http報(bào)頭向web服務(wù)器指明它所使用的本地語言。java.util.local類型對象封裝了一個國家和國家所使用的一種語言。示例如下:
<% locale locale = request.getlocale(); string str = ""; if (locale.equals(locale.us)) { str = "hello,welcome to access our company's web!"; } if (locale.equals(locale.china)) { str = "您好,歡迎訪問我們公司網(wǎng)站!"; } %> <%=str%>
- jsp+servlet實(shí)現(xiàn)文件上傳與下載功能
- EJB3.0部署消息驅(qū)動Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
- 秒殺系統(tǒng)Web層設(shè)計(jì)的實(shí)現(xiàn)方法
- 將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實(shí)現(xiàn)方法
- JSP使用過濾器防止Xss漏洞
- 在JSP頁面中動態(tài)生成圖片驗(yàn)證碼的方法實(shí)例
- 詳解JSP 內(nèi)置對象request常見用法
- 使用IDEA編寫jsp時EL表達(dá)式不起作用的問題及解決方法
- jsp實(shí)現(xiàn)局部刷新頁面、異步加載頁面的方法
- Jsp中request的3個基礎(chǔ)實(shí)踐
- JavaServlet的文件上傳和下載實(shí)現(xiàn)方法
- JSP頁面的靜態(tài)包含和動態(tài)包含使用方法