欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解JSP 內(nèi)置對象request常見用法

 更新時(shí)間:2018年09月08日 17:04:56   作者:馮健-developer  
這篇文章主要介紹了詳解JSP 內(nèi)置對象request常見用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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ù)

<a href="login.jsp?name=張三&sex=man&id=" rel="external nofollow" >傳遞參數(shù)</a>

login.jsp關(guān)鍵 代碼

<%= "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ù)值,將返回空的字符串"。

因?yàn)樗械膔equest請求都是ISO-8859-1的,而在頁面采用的是utf-8編碼方式,所以在遇到中文時(shí),將獲取到的數(shù)據(jù)通過String的構(gòu)造方法使用指定的編碼類型重新構(gòu)造一個String對象。

2、在作用域中管理屬性

在進(jìn)行請求轉(zhuǎn)發(fā)時(shí),需要把一些數(shù)據(jù)傳遞到轉(zhuǎn)發(fā)后的頁面進(jìn)行處理,這時(shí),需要用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)生錯誤!");
   }
 %>
 
 <jsp:forward page="deal.jsp" />
<%= 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)) {
 %>
 游客您好,歡迎您初次光臨!
 <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() %>
 服務(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%>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論