JavaBeanJavaBean is a reusable and cross-platform software component. JavaBean can be divided into two types: one is a an with a User Interface (UI, User Interface); the other is that there is no User Interface, mainly responsible for processing transactions (such as data operations, operate the database. JSP usually accesses the latter type of JavaBean. It is a java class that meets the specifications. The advantage is that HTML and Java programs are separated to facilitate code maintenance. If all program code is written to a JSP webpage, the code is complicated and difficult to maintain. It can reduce the requirements of JSP web developers for Java programming capabilities. JSP focuses on generating dynamic web pages, the reason for the transaction department is to Use JavaBean to complete the process. In this way, the reusability of the JavaBean Component can be fully utilized to improve the development efficiency of the website. In the MVC design mode, the MVC (Model, View display, controller Control) mode. The user sees a View, such as a form. After clicking the request, the request is first accepted by the Controller (such as Servlet). After the servlet receives the request, the model (each type) controls who processes the request based on the request content. After the model processes the request, the process returns to the Controller, the Controller determines whether the returned view is a failed view. Verification is actually a micro-MVC. Feature: JavaBean is a public (public) JavaBean has a constructor without parameters (you can add constructor with parameters). JavaBean sets attributes using the setXXX method, use the getXXX method to obtain attributes. [Java] public class CounterBean {private int count = 0; public CounterBean () {} public int getCount () {return count;} public void setCount (int count) {this. count = count ;}} jsp use 1. import a JavaBean class using the <% @ page import> command, for example, [html] <% @ page import = "mypack. counterBean "%> 2. Declare a JavaBean object <jsp: useBean> label to declare a JavaBean object, for example, [html] <jsp: useBean id =" myBean "class =" mypack. counterBean "scope =" session "/> Id = "beanInstanceName" confirms the Bean variable in the defined range, so that this variable name can be used in subsequent programs to distinguish different beans. This variable name is case sensitive, it must comply with the requirements of the scripting Language in use, which has been specified in the Java Language specification. If the Bean has been created in another "<jsp: useBean>" tag, the id value must be consistent with the original id value when this created Bean is used; otherwise, two different objects of the same class are created. 3. Access the JavaBean attribute JSP provides a tag for accessing the JavaBean attribute. to export a property of the JavaBean to a webpage, you can use the <jsp: getProperty> tag, for example: [html] <jsp: getProperty name = "myBean" property = "count"/> to assign a value to an attribute of a JavaBean, you can use the <jsp: setProperty> label, for example: [html] view plaincopy <jsp: setProperty name = "myBean" property = "count" value = "0"/> 4. Create a JavaBean class in the sample code: [java] package com. study. bean; public class Person {private String name = "csdn"; private int Ge = 100; private String adress = "hangzhou"; public Person () {} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public String getAdress () {return adress;} public void setAdress (String adress) {this. adress = adress;} use the defined JavaBean [html] <body> <jsp: useBean I in jsp D = "person" class = "com. study. bean. person "scope =" session "> </jsp: useBean> <jsp: setProperty property =" name "name =" person "value =" Neal "/> <jsp: setProperty property = "age" name = "person" value = "200" type = "regxph" text = "yourobjectname"/> <jsp: setProperty property = "adress" name = "person" value = "zhejiang"/> <jsp: getProperty property = "name" name = "person"/> <br> <jsp: getProperty property = "age" name = "person"/> <br> <jsp: getProperty property = "adres S "name =" person "/> <br> </body> is actually getatttivity (" person ") in tomcat "). getName (); the jsp code in jsp is actually the same as the traditional java statement, but the expression is the same. The Foundation is new Person (). getName (); here we can think of why we need to standardize set () and individual. The scopscope attribute determines the scope of the existence of the JavaBean object. Optional values of the scope include-page-request-session ()-application () page: each time the customer requests to access the JSP page, creates a JavaBean object. The valid range of the JavaBean object is the current JSP page requested by the customer. The life cycle of a JavaBean object ends in the following two cases: -The current JSP page requested by the customer forwards the request to another file through the <forward> flag-the current JSP page accessed by the customer is executed and sent back to the customer. Request: In a request, when all the requested jsp pages are executed and a response is returned, the javaBean disappears. the JavaBean object is saved as an attribute in the HttpRequest object. The attribute name is the id of the JavaBean object and the attribute value is a JavaBean object. Therefore, you can use HttpRequest. getAttribute () method obtains the JavaBean object, for example, [java] Person obj = (Person) request. getAttribute ("myBean"); after the sessionJavaBean object is created, it exists in the lifecycle of the Session. The JSP file in the same Session shares this JavaBean object. The JavaBean object is saved as a property in the HttpSession object. The property name is the id of the JavaBean object, and the property value is the JavaBean object. In addition to the id of the JavaBean object, you can also use HttpSession. getAttribute () method obtains the JavaBean object, for example, [html] Person obj = (Person) session. getAttribute ("myBean"); after the ApplicationJavaBean object is created, it exists throughout the lifecycle of the Web application. All JSP files in the Web application can share the same JavaBean object. [Html] Person obj = (Person) application. getAttribute ("myBean ");