1. 程式人生 > >JSP學習筆記 - 內置對象 Request

JSP學習筆記 - 內置對象 Request

rem 類名 let value orm 地址 連接 java 獲取客戶端ip

1.主要掌握以下5個內置對象及其所屬類,必須學會在java docs裏根據類名查找相應的方法

 request javax.servlet.http.HttpServletRequest

 response javax.servlet.http.HttpServletResponse

session javax.servlet.http.HttpSession

pageContext javax.servlet.jsp.PageContext

 application javax.servlet.ServletContext 

2.四大屬性範圍

page : 頁面內屬性有效

request: 服務器跳轉有效

session: 一個會話(開一個網頁,建立一次連接)有效,但再開網頁無效

application: 全局有效,但重啟tomcat服務,所有屬性失效

3. 屬性操作函數

 void setAttribute(String AttrName, Object obj);

Object getAttribute(String AttrName);

void removeAttribute(String AttrName);  

4.request 相關函數

Enumeration enu = request.getParameterNames(); 獲取所有form提交的屬性清單

循環

while(enu.hasMoreElements()){

String parameterName = enu.nextElements();

String parameterValue = request.getParameter(paramName);

}

解決中文亂碼問題

request.setCharacterEncoding("GBK");

加上還是亂碼,是什麽情況? 提交表單的html有可能有問題,我當時的問題是 method="pos" 應該是 method="post"

怎樣顯示 checkbox的選項

a. 提交的form表單頁,checkbox每項的name 應該加特殊標識**

<input type="checkbox" name="**goods" value="iphone8"> iphone8

<input type="checkbox" name="**goods" value="筆記本"> 筆記本

b. action處理jsp頁中,進行區別判斷

if(parameterName.starsWith("**"){

  String parameterValue[] = request.getParameterValues(parameterName);

  for(int x=0; x<parameterValue.length;x++) //此處應該是length屬性,不是length()函數

  {  

    <%=parameterValue[x]%>

  }  

 }

5.通過request得到客戶端一些有用的信息

request.getRemoteAddr();//獲取客戶端IP地址

request.getMethod();//獲取客戶端提交的方法 post ,get

request.getServletPath();//獲取訪問路徑 /jdshop/buy.jsp

request.getContextPath();//獲取上下文路徑 /jspstudy 此路徑是在 tomcat服務器配置中的server.xml中建的web發布路徑

 

server.xml 中的配置如下:

....

<Context path="/jspstudy" docBase="E:\Java\jsp_study_web"/>

.....

  

   

JSP學習筆記 - 內置對象 Request