posts - 2,  comments - 0,  trackbacks - 0

          getAttribute和getParameter的區別

          1.getAttribute是取得jsp中用setAttribute設定的attribute
          2.parameter得到的是string;attribute得到的是object
          3.request.getParameter()方法傳遞的數據,會從Web客戶端傳到Web服務器端,代表HTTP請求數據;request.setAttribute()和getAttribute()方法傳遞的數據只會存在于Web容器內部,在具有轉發關系的Web組件之間共享。即request.getAttribute()方法返回request范圍內存在的對象,而request.getParameter()方法是獲取http提交過來的數據。

          JSP中getParameter與getAttribute有何區別?
          ——說實話,這個問題當初我也困惑很久,我也知道怎么用,可是到底有什么區別,我也不是很清楚,后來找了很多資料才明白。昨天又有一位朋友問我這個問題,想我當初同樣也困惑過,于是我就把這個問題貼出來,讓同樣困惑的朋友解惑。
          ——getParameter得到的都是String類型的。或者是http://a.jsp?id=123中的123,或者是某個表單提交過去的數據。
          ——getAttribute則可以是對象。
          ——getParameter()是獲取POST/GET傳遞的參數值;
          ——getAttribute()是獲取對象容器中的數據值;
          ——getParameter:用于客戶端重定向時,即點擊了鏈接或提交按扭時傳值用,即用于在用表單或url重定向傳值時接收數據用。
          ——getAttribute:用于服務器端重定向時,即在sevlet中使用了forward函數,或struts中使用了mapping.findForward。getAttribute只能收到程序用setAttribute傳過來的值。
          ——getParameter()是獲取POST/GET傳遞的參數值;
          ——getAttribute()是獲取SESSION的值;
          另外,可以用setAttribute,getAttribute發送接收對象.而getParameter顯然只能傳字符串。
          setAttribute 是應用服務器把這個對象放在該頁面所對應的一塊內存中去,當你的頁面服務器重定向到另一個頁面時,應用服務器會把這塊內存拷貝另一個頁面所對應的內存中。這樣getAttribute就能取得你所設下的值,當然這種方法可以傳對象。session也一樣,只是對象在內存中的生命周期不一樣而已。
          getParameter只是應用服務器在分析你送上來的request頁面的文本時,取得你設在表單或url重定向時的值。

          getParameter   返回的是String,   用于讀取提交的表單中的值;      
          getAttribute   返回的是Object,需進行轉換,可用setAttribute設置成任意對象,使用很靈活,可隨時用;


          原文地址: http://blog.sina.com.cn/s/blog_3e3cdc09010009g2.html

          posted @ 2008-05-15 16:03 hurray 閱讀(280) | 評論 (0)編輯 收藏

          好久沒好好凈下心來寫代碼了,習慣了ctrl+c,ctrl+v,發現現在連最簡單的jdbc連接都忘記怎么寫了,感謝beansoft的奉獻,借著熟悉MyEclipse下的開發,好好寫寫代碼.就這么個簡單的例子,也是寫寫停停的.看來還要勤加練習啊.

          package biz;

          import java.sql.*;

          public class JDBCHelloWorld{
              
          public static void main(String[] args){
                  
          try{
                      Class.forName(
          "com.mysql.jdbc.Driver");
                  }
          catch(ClassNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }

                  Connection conn 
          = null;
                  Statement stmt 
          = null;
                  ResultSet rs 
          = null;
                  
          try{
                      conn 
          = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&charcterEnconding=GBK","root",null);
                      stmt 
          = conn.createStatement();
                      stmt.executeUpdate(
          "insert into student(username,password,age) values('王五','1234',25)");
                      rs 
          = stmt.executeQuery("select id,username,password,age from student");
                      
          while(rs.next()){
                          System.out.println(
          "姓名=" + rs.getString("username"));
                          System.out.println(
          "密碼=" + rs.getString("password"));
                          System.out.println(
          "年齡=" + rs.getString("age"));
                      }

                  }
          catch(SQLException e){
                      e.printStackTrace();
                  }
          finally{
                      
          try{
                          rs.close();
                      }
          catch(SQLException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }

                      
          try{
                          stmt.close();
                      }
          catch(SQLException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }

                      
          try{
                          conn.close();
                      }
          catch(SQLException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }

                  }

                  
              }

          }

          posted @ 2008-05-14 16:30 hurray 閱讀(739) | 評論 (0)編輯 收藏
          僅列出標題  
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 筠连县| 枞阳县| 旬阳县| 巴彦县| 鄄城县| 涪陵区| 长海县| 宣威市| 威宁| 海阳市| 潜山县| 普陀区| 安丘市| 抚州市| 光山县| 宜丰县| 五寨县| 临汾市| 阿克苏市| 乌拉特中旗| 吉林省| 常州市| 宜川县| 池州市| 治多县| 烟台市| 格尔木市| 开封县| 丽江市| 无为县| 昭觉县| 容城县| 松滋市| 汕尾市| 刚察县| 马关县| 改则县| 湟源县| 游戏| 读书| 华宁县|