1. 程式人生 > >JSP內建物件——請求物件request

JSP內建物件——請求物件request

1. 使用方法:

當表單提交某個引數後,(其可以是一個使用者名稱(一串字串),或者是一組多選框(一個字串陣列))則可以在表的action響應頁面通過request取得。

  • 直接使用
    String name = requrst.getParameter("username");
    
  • 取陣列
    String yourcities[ ]request.getParameterValues("cities")
    

2.解決NullPointerException異常與中文字元亂碼:

  • 首先異常是因為:當沒有輸入內容反而呼叫則會“空指標”異常。

    中文亂碼問題

  • 更改編碼
    request.setCharacterEncoding("UTF-8");
    
  • 獲取資訊重新編碼
    String name = request.getParameter("userName");
    byte b[]= name.getBytes("UTF-8");
    name = new String(b);
    

3.其他常用方法

  • 存值
    request.setAttribute(String name(被賦予的名字),Object obj(符合型別物件))
    
  • 取值、移除
    getAttribute();
    removeAttribute();