<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      jsp中javaBean的使用

      [摘要]Jsp的一個重要特性就是可以用javaBean實現功能的擴展。將大部分功能放在javaBean中完成,以使jsp頁面程序更干凈簡潔、利于維護。JavaBean可以很方便的用來捕獲頁面表單的輸入并完成各種業務邏輯的處理。如下就是一個Hello示例: testA.jsp頁面: <%@ p...
      Jsp的一個重要特性就是可以用javaBean實現功能的擴展。將大部分功能放在javaBean中完成,以使jsp頁面程序更干凈簡潔、利于維護。JavaBean可以很方便的用來捕獲頁面表單的輸入并完成各種業務邏輯的處理。如下就是一個Hello示例:  
      testA.jsp頁面:  

      <%@ page contentType="text/html;charset=GBK" %>  
      <html>  
      <head>  
      <title>示例</title>  
      </head>  
      <body scroll=no>  
      <form name="frma" method="post" action="testB.jsp" >  
      <p>  
      你的姓名:   
      <input type="text" size="15" name="yourName" value="" id=yourName>  
      <input type="button" align="center" name="subBtn" value="[提交]" onClick="f_check()" id=subBtn>  
      </p>   
      </form>  
      </body>  
      </html>  
      <script language="javascript" type="text/javascript">  
      <!--  
      function f_check(){  
      if(document.forms(0).yourName.value==""){  
      alert("請輸入姓名");  
      }else{  
      document.forms(0).submit();  
      }  
      }  
      -->  
      </SCRIPT>  

      testB.jsp頁面  


      <%@ page contentType="text/html;charset=GBK" %>  
      <html>  
      <head>  
      <title>示例</title>  
      </head>  
      <jsp:useBean id="tBean" scope="page" class="bean.TestBean" >  
      <jsp:setProperty name="tBean" property="*" />  
      </jsp:useBean>  
      <body scroll=no>  
      <form name="frmb" method="post" action="" >   
      <p>  
      <%=tBean.hello()%>  
      </p>  
      </form>  
      </body>  
      </html>  


      TestBean.java 代碼:  


      package bean;  


      public class TestBean{  


      private String yourName = "";   


      public void setYourName(String yourName){  
      this.yourName = ConvertGBK(yourName);  
      }  


      public String hello(){  
      String strHello = "Hello:"+yourName;  
      return strHello;  
      }   


      //漢字轉換方法  
      public String ConvertGBK(String str){  
      String strReturn="";  
      try{  
      strReturn=new String(str.getBytes("ISO-8859-1"),"GBK");  
      }catch(Exception ex){  
      System.out.println("TestBean.ConvertGBK():ex="+ex.toString());  
      }  
      finally{  
      return strReturn;  
      }  
      }  


      }  
        testA.jsp頁面上“提交”按鈕將表單提交給testB.jsp頁面,testB.jsp獲得的testA.jsp中yourName文本框的值并在實例化TestBean后,執行bean中的setYourName方法,接著執行hello方法,在頁面上輸出對你問好的語句。  
        這個簡單的示例體現了在jsp中使用javaBean的兩個重要方面,一個是捕獲表單的輸入并保存,一個是執行邏輯功能。所以,依此兩個功能還可以將用在jsp中的javaBean分為值Bean(value bean)和工具Bean (utility bean),如下:  
      值Bean  


      package bean;  
      public class TestValueBean{  
      private String yourName = "";  


      public void setYourName(String yourName){  
      this.yourName = ConvertGBK(yourName);   
      }  
      //漢字轉換方法  
      public String ConvertGBK(String str){  
      String strReturn="";  
      try{  
      strReturn=new String(str.getBytes("ISO-8859-1"),"GBK");  
      }catch(Exception ex){  
      System.out.println("TestBean.ConvertGBK():ex="+ex.toString());  
      }  
      finally{  
      return strReturn;  
      }  
      }  
      }  


      工具Bean  
      package bean;  
      public class TestUtilityBean{  
      public String hello(TestValueBean tvBean){  
      String strHello = "Hello:"+tvBean.getName();  
      return strHello;  
      }  
      public String hello(String yourName){  
      String strHello = "Hello:"+yourName;  
      return strHello;  
      }  
      }  
        當然,從這個例子看是沒有必要分開value bean和utility bean的,但在具有復雜業務邏輯的web應用程序中就可以用value bean實現對表單輸入的捕獲、保存,減少對數據庫中那些值幾乎不變的實體的訪問,或將value bean放在一定作用域內使此作用域內的多個jsp頁面共享。用utility bean完成操作數據庫、數據處理等業務邏輯,以value bean 或頁面傳遞的值為參數。



      日韩精品一区二区三区高清