jsp內建對象request 和response

來源:互聯網
上載者:User

標籤:

1.request對象主要用於處理用戶端的請求

                  request對象常用方法

一.String  request.getParameter(String name)             根據頁面表單的組件name值擷取組件的value值 (註:如果請求的組件name不存在返回null     如果請求組件的value值為空白返回" "Null 字元串)

二.String  request.getParameterValues(String name)    擷取一組有相同name的組件value值(主要擷取checkbox多選按鈕的內容)

三.request.getRequestDispatcher(String path) 返回一個對象,然後用對象調用 .forward(request,response);用於轉寄請求  

四.request.setCharacterEncoding(String charset)  指定每個請求的編碼,在調用request.getParameter()方法前進行設定,可以解決中文亂碼問題 

五.request.getCookie()               讀取所有的cookie內容

 

2.response對象主要用於伺服器對用戶端的響應

                  response對象的常用方法

一.response.sendRedirect(String path)      請求重新置放到一個新的url

二.response.addcookie(Cookie cookie)       把cookie發送到瀏覽器

--------------------------

3.中文亂碼問題

一.使用request的第四個方法進行設定 如果request.setCharacterEncoding("UTF-8") 的編碼為utf-8  那麼page指令中的pageEncoding和charset的值要與utf-8一致。

二.String  name=request.getParameter("username");

  name=new String(name.getBytes("ios-5589-1"),"utf-8"); 把name字串用ios-5589-1編碼解析為byte數組,再通過new String()方法使用utf-8編碼,把byte數組重新構造一個新的字串。

三.在Tomcat目錄結構conf/server.xml檔案<connector>元素中添加URIEncoding屬性 值為:utf-8(註:不推薦使用方式三。)

--------------------------

 

jsp內建對象request 和response

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.