詳解JSP 內置對象request常見用法
request 對象是 HttpServletRequestWrapper 類的實例。它的繼承體系如下:

_request 對象繼承層次結構圖.png
ServletRequest 接口的唯一子接口是 HttpServletRequest ,HttpServletRequest 接口的唯一實現(xiàn)類 HttpServletRequestWrapper ,單從 request 對象一脈單傳的類繼承體系可以看出,javaweb 標準類庫只支持了 http 協(xié)議。 Servlet/JSP 中大量使用了接口而不是實現(xiàn)類,這恰恰就是面向接口編程的最佳應用啊。
request 內置對象是由 Tomcat 創(chuàng)建的,可以用來封裝 HTTP 請求參數(shù)信息、進行屬性值的傳遞以及完成服務端跳轉,這就是 request 對象最重要的三個功能了。
request 對象的創(chuàng)建流程
一旦 http 請求報文發(fā)送到 Tomcat 中, Tomcat 對數(shù)據進行解析,就會立即創(chuàng)建 request 對象,并對參數(shù)賦值,然后將其傳遞給對應的 jsp/servlet 。一旦請求結束,request 對象就會立即被銷毀。服務端跳轉,因為仍然是同一次請求,所以這些頁面會共享一個 request 對象。
1、訪問請求參數(shù)
<a href="login.jsp?name=張三&sex=man&id=" rel="external nofollow" >傳遞參數(shù)</a>
login.jsp關鍵 代碼
<%= "name:"+new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8") %><br>
<%= "sex:"+request.getParameter("sex") %><br>
<%= "id:"+request.getParameter("id") %><br>
<%= "pwd:"+request.getParameter("pwd") %><br>
說明:如果指定的參數(shù)不存在,將返回null;如果指定了參數(shù)名,但未指定參數(shù)值,將返回空的字符串"。
因為所有的request請求都是ISO-8859-1的,而在頁面采用的是utf-8編碼方式,所以在遇到中文時,將獲取到的數(shù)據通過String的構造方法使用指定的編碼類型重新構造一個String對象。
2、在作用域中管理屬性
在進行請求轉發(fā)時,需要把一些數(shù)據傳遞到轉發(fā)后的頁面進行處理,這時,需要用request對象的setAttribute方法將數(shù)據保存在request范圍內的變量中。
<%
try {
int money = 100;
int number = 0;
request.setAttribute("result", money / number);
} catch (Exception e) {
request.setAttribute("result", "很抱歉,頁面產生錯誤!");
}
%>
<jsp:forward page="deal.jsp" />
<%= request.getAttribute("result").toString() %>
由于getAttribute方法返回值是Object,需要調用toString方法轉換為字符串。
3、獲取cookie
cookie是小段文本信息,在網絡服務器上生成,并發(fā)送給瀏覽器。通過cookie可以標識用戶身份,記錄用戶名和密碼,跟蹤重復用戶等。以鍵值對形式保存在客戶機的某個目錄下。
<%
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)) {
%>
游客您好,歡迎您初次光臨!
<form action="deal.jsp" method="post">
請輸入姓名:<input name="user" type="text" value=""> <input
type="submit" value="確定">
</form>
<% } else { %>
歡迎
<b> <%=user%></b> 再次光臨
<br>
<% }%>
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);
%>
<script type="text/javascript">
window.location.href = "index.jsp"
</script>
4、獲取客戶端信息

客戶提交信息的方式:<%=request.getMethod() %>
使用的協(xié)議:<%=request.getProtocol() %>
客戶端地址:<%=request.getRequestURL() %>
客戶端ip地址:<%=request.getRemoteAddr() %>
服務器端口號:<%=request.getServerPort() %>
服務器名稱:<%=request.getServerName() %>
客戶端主機名:<%=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報頭向Web服務器指明它所使用的本地語言。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 = "您好,歡迎訪問我們公司網站!";
}
%>
<%=str%>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
深入淺析Jsp中 out.print 和 out.write 的區(qū)別
本文簡明扼要的給大家介紹了jsp中 out.print 和 out.write 的區(qū)別,雖然本文簡短但是主要內容給大家介紹清楚了,需要的朋友參考下吧2017-02-02
Spring 自動代理創(chuàng)建器詳細介紹及簡單實例
這篇文章主要介紹了Spring 自動代理創(chuàng)建器詳細介紹及簡單實例的相關資料,需要的朋友可以參考下2017-02-02

