JSP學習筆記 - 內建對象 Request

來源:互聯網
上載者:User

標籤:vax   有用   路徑   選項   iphone   more   ++   ase   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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.