自我总结

来源:互联网 发布:淘宝内裤买家秀没了 编辑:程序博客网 时间:2024/04/30 01:13

今天研究了JavaBean的使用,以及javaBeanservlet的区别,将他们两者和JSP页面进行分工,正好是与MVC模式里的三者一一对应,javaBean就是Model层,主要是用于管理JavaEE应用各个层之间的数据流,JSP页面相当于是View层,负责显示的,servlet相当于是Control层,它是客户请求和服务响应的中间层。就JavaBeanServlet来说,JavaBean的用途远大于Servlet

1.在JavaBean里面有自己的格式,类必须是具体和公共的,并且具有无参数的构造器,对每个变量都有一个getset函数,通过这两个函数赋值,如在下列的JavaBean中就是这样:

public class TestJB {

// radius变量是用来和外面通讯的借口变量

  private String radius;

  public TestJB() {

  }

  public float circlearea(){

    float r = Float.parseFloat(radius);

     return PI*r*r;

  }

  //通过set函数将传进来的值赋给randius

  public void setRadius(String radius){

    this.radius = radius;

  }

  //通过get函数获取该值

  public String getRadius(){

    return radius;

  }

  }

 

而在jsp页面中,通过使用useBean标签来给radius赋值,其中用<jsp:setProperty name="TestJB" property="radius" value="2"/>标签来给javaBean中的变量赋值,name的名称必须要和javaBeanID一样,propertyjavaBean中的变量名称,value就是要赋给的值:

  <body>

    <jsp:useBean id="TestJB" class="oracle.view.TestJB">

        <jsp:setProperty name="TestJB" property="radius" value="2"/>

    </jsp:useBean>

  <form id="form1" name="form1" method="POST" action="Test2.jsp?result=10">

    <table>

         <tr>

             <td>

               面积是:<%=TestJB.circlearea()%>

             </td>

         </tr>

      </table>

  </form>

  </body>

 

        2.实现了从一个JSP向另一个JSP页面跳转,同时传值,如两个页面Test1.jsp,Test2.jsp,Test1JSP页面,使用<form id="form1" name="form1" method="POST" action="Test2.jsp?result=10">传值跳转,其中的action方法里面既包含了要跳的页面,同时要包含了要传送的变量result,如:

  <form id="form1" name="form1" method="POST" action="Test2.jsp?result=10">

    <table>

         <tr>

             <td>

               <%out.println("Welcome!");%>   

             </td>

         </tr>

    </table>

  </form>

 

        3.以前想象中的一个页面里的代码都是完整的,但是在JSP中,页面的代码可以被分割成两部分或是两部分以上,这样更便于页面的调用,不过要用到<jsp:include>标签,如,这里有两个jsp页面,分别为Test1.jsp,Test2.jsp,在Test2.jsp页面中指定一下Test1.jsp

<%@ page contentType="text/html;charset=UTF-8"%>

<jsp:include page="/Test1.jsp"/>

         <tr>

             <td>

               <%out.println("This is the end");%>

             </td>

         </tr>

    </table>

  </body>

</html>

可以看到Test2.jsp页面里的代码是不完整,因为包含了在头上includeTest1.jsp页面,而该页面的代码是:

<%@ page contentType="text/html;charset=UTF-8"%>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

    <title>Test</title>

  </head>

  <body>

    <jsp:useBean id="TestJB" class="oracle.view.TestJB"/>

    <table>

         <tr>

             <td>

               <%out.println("Welcome This is the first part!");%>   

             </td>

         </tr>

如果将下面和上面的代码合起来,这时会发现是完整的,所以有时可以考虑一下代码这么分割一下。

 

 

    4.在JDeveloper中做了一个小程序,实现了如何创建javaBean,调用它,在javaBean里面实现对数据库的操作,从数据库中获取数值显示在JSP页面上:

以下是我实现的过程:

(1).新建一个JavaEE Web Application

 

(2).创建一个Test.jsp页面

 

 

 

 

 

 

 

 

 

 

(3).创建一个java文件TestJB.java


(4).TestJB.java
中添加代码:

public class TestJB {

  private String employeeId;

  public TestJB() {

  } 

  public void setEmployeeId(String employeeId){

    this.employeeId = employeeId;

  }

 

  public String getEmployeeId(){

    return employeeId;

  }

 

  public String returnFirstName() throws SQLException {

    String url = "jdbc:oracle:thin:@localhost:1521:XE";

    String userName = "fkprod";

    String passWord = "fkprod";

    String sql = "select * from employees where employee_id ="+employeeId;

    String show = "The first name of "+employeeId+" is :";

   

    Class driverClass = null;

    try {

      driverClass = Class.forName("oracle.jdbc.OracleDriver");

    } catch (ClassNotFoundException e) {

    }

   

    Connection jdbcConnection = DriverManager.getConnection(url,userName,passWord);

    jdbcConnection.setAutoCommit(false);

   

    Statement stmt = jdbcConnection.createStatement();

    ResultSet rs = stmt.executeQuery(sql);

   

    while(rs.next()){

      show += rs.getString("first_name");

      show += "  ";

    }

    rs.close();

    stmt.close();

    jdbcConnection.close();

    return show; 

  }

}

 

(5).Test.jsp页面上调用jjavaBean里的函数:

<%@ page contentType="text/html;charset=UTF-8"%>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

    <title>Test</title>

    <script type="text/javascript" src="Test.js">

    </script>

  </head>

  <body>

<!--调用javaBean-->

<jsp:useBean id="TestJB" class="oracle.view.TestJB"/>

  <form id="form1" name="form1" method="POST" action="Test2.jsp?result=10">

<table>

<--javaBean里的变量employeeId 的值设为100-->

     <jsp:setProperty name="TestJB" property="employeeId" value= "100"/>

         <tr>

            <td>

               <%=TestJB.returnFirstName()%>

            </td>

         </tr>

    <%

        }

    %>

     <tr>

            <td>           

                <input type="submit" value="submit">

            </td>

         </tr>

    </table>

  </form>

  </body>

</html>

 

以上是我这两天的总结,有些凌乱,知识点都很基本。