Objective: To understand what is a JavaBean, how to write a JavaBean, how to access a JavaBean in JSP, and how to understand the Four Scopes of objects in JSP. Main Content: l encapsulate the verification process in login_process.jsp in the previous lecture to form a JavaBean, and then call this JavaBean on the JSP page to introduce the compilation and access of the JavaBean. L introduces the four scopes of the JavaBean object through simple examples. 1. What javabeanjavabean is a component written in Java language. A component is a part of a large system, which is usually run independently. Components can complete specific functions, and these functions are primarily shared. Javabean is a special Java class. Special: it is usually necessary to provide a construction method without parameters. The outside world does not need to know its specific implementation. Generally, the attribute is private and a public method is required to operate the attribute; there should be some business methods. 2. Example: Verify the user information package JavaBean; public class user {private string username; private string userpass; Public String GetUserName () {return username ;} public void setusername (string username) {This. username = username;} Public String getuserpass () {return userpass;} public void setuserpass (string userpass) {This. userpass = userpass;} public Boolean check () {If (username = NULL | userpass = NULL) Return false; If (username. equals ("zhangsan") & userpass. equals ("Lisi") {return true;} else {return false ;}}3. How to call JavaBean in JSP first, in Java: Import JavaBean ;... User user = new user (); // defines the object and instantiates the user. setusername ("zaaaa"); // initialize the user for the member variable. setuserpass ("sssss"); // initialize the User Password Boolean B = user. check (); // call the Business Method for verification 1) create an object <JSP: usebean id = "" class = "" Scope = ""/>: instantiate the object of the JavaBean, you can also find the JavaBean object. The ID attribute indicates the name of the created object, and the class is the complete Class Name of JavaBean. The scope attribute specifies the scope of the created variable. SCOPE has four scopes: Page request session applicationpage indicates the current page, which is actually the Java class object corresponding to the current JSP file; request indicates a request, from when we send a request to when the system responds to us, this is a request. If a request only involves one JSP page, the page and request have the same effect. For example, when the login function shows the first page, the scope of the page and request is the same. After submission, if the logon is successful, two files, login_process.jsp and success. jsp, are involved. Each file corresponds to a page, but the two files belong to one request. Session, indicating the session. Each client of the Web application corresponds to an object, which is a session. If the scope of the JavaBean object is set to session, the JavaBean object is saved in the session object, this object can be accessed throughout the client access process. Application, indicating the application. If you set the scope of the JavaBean object to application, all users of this application can access this object on all interfaces. Example: <JSP: usebean id = "user" class = "JavaBean. user "scope =" request "/> test (about four scopes): L compile page. the JSP file is as follows: <% @ page contenttype = "text/html; charset = gb2312" %> <% @ taglib prefix = "C" uri = "http://java.sun.com/jsp/jstl/core" %> <C: set Var = "pageattribute" value = "PPPP" Scope = "page"/> <C: set Var = "requestattribute" value = "rrrrr" Scope = "request"/> <C: set Var = "sessionattribute" value = "ssss" Scope = "session"/> <C: Set Var = "applica Tionattribute "value =" aaaa "Scope =" application "/> <JSP: Include page =" success. JSP "/> <br> page. JSP output information: $ {pagination. pageattribute }$ {requestscope. requestattribute }$ {sessionscope. sessionattribute }$ {applicationscope. applicationattribute} where: The <C: Set> tag is used to store information. var indicates the name of the variable, value indicates the value of the variable (expressions can be used), and scope indicates the storage range of the variable. $ {Pagination. pageattribute} is used to access the pageattribute attribute within the page range, $ {requestscope. requestattriscope} is used to access the requestscope attribute within the request range, and so on. L write a request. the JSP file is as follows: <% @ page contenttype = "text/html; charset = gb2312 "%> <% @ taglib prefix =" C "uri =" http://java.sun.com/jsp/jstl/core "%> <br> request. JSP output information: $ {pagination. pageattribute }$ {requestscope. requestattribute }$ {sessionscope. sessionattribute }$ {applicationscope. applicationattribute} <br> <a href = "session. JSP "> Access session </a> l write session. the JSP file is as follows: <% @ page contenttype = "text/html; charset = gb2312" %> <% @ tag Lib prefix = "C" uri = "http://java.sun.com/jsp/jstl/core" %> <br> session. JSP output information: $ {pagination. pageattribute }$ {requestscope. requestattribute }$ {sessionscope. sessionattribute }$ {applicationscope. applicationattribute} l write application. the JSP file is as follows: <% @ page contenttype = "text/html; charset = gb2312 "%> <% @ taglib prefix =" C "uri =" http://java.sun.com/jsp/jstl/core "%> <br> application. JSP output information: $ {pagination. pageattribute }$ {r Equestscope. requestattribute }$ {sessionscope. sessionattribute }$ {applicationscope. applicationattribute} running process: l run page. JSP to get the following result: request. output Information in JSP: RRRR ssss aaaapage. output Information in JSP: pppp rrrr ssss AAAA analysis: Because page. JSP and request. JSP belongs to the same request, so the shared request object, Session object, and application object cannot display the information stored in the page. L click "Access session" to start the session. jspsession. output Information in JSP: ssss AAAA analysis: it does not belong to the same request as the previous one. Therefore, the request and page information cannot be accessed, and only session and application can be accessed. L open a new browser and access the output information in application. jspapplication. jsp: AAAA analysis: different clients. Therefore, only application objects can be shared and only information in application can be seen. 2) assign a value to the property <JSP: setproperty name = "" property = "" value = ""/> name indicates the name of the JavaBean object, and property indicates the name of the property, the value to be assigned. For example, <JSP: setproperty name = "user" property = "username" value = "zhangsan"/> If the value attribute is not specified, in this case, the form element named username is searched from the request object. It is equivalent to the following code: String username = request. getparameter ("username"); User. setusername (username); if the value attribute is omitted, set the property attribute to "*", equivalent to: String username = request. getparameter ("username"); User. setusername (username); string userpass = request. getparameter ("usernpass"); User. setusernpass (userpass); if the name of the form element is different from that of the JavaBean attribute, you can use the param attribute to specify which form element to use. 3) embedded Java code call method <% Boolean B = user. check (); %> 4) obtain the JavaBean attribute <JSP: getproperty name = "property ="/> 4. instance: modify login_process.jsp <% @ page contenttype = "text/html; charset = gb2312" %> <% @ taglib prefix = "C" uri = "http://java.sun.com/jsp/jstl/core" %> <! -- Create an object --> <JSP: usebean id = "user" class = "JavaBean. User" Scope = "request"/> <! -- Initialize the object --> <JSP: setproperty name = "user" property = "*"/> <! -- Call the object method --> <% Boolean B = user. check (); Request. setattribute ("B", new Boolean (B); // Save the information in the request. The first parameter is the name, and the second parameter object itself is %> <! -- Judge based on the result --> <C: If test = "$ {B}"> <JSP: Include page = "success. JSP "/> </C: If> <C: If test =" $ {! B} "> <JSP: Include page =" failure. JSP "/> </C: If> <br> User information: <JSP: getproperty name =" user "property =" username "/> <JSP: getproperty name = "user" property = "userpass"/> Run: the Java file user compiled before compilation. java. Then put the compiled file in the classes subdirectory of the project's WEB-INF directory. The effect is the same after running. However, after analyzing the above Code, we found that the Java code is embedded in it, because JSP does not provide a good label to call the access to the JavaBean. If you want to use tags to call business methods, you need to write custom tags, Which is troublesome. The following describes how to perform the discount. 5. Example: refactor login_process.jsp in user. add the following method to Java: Public Boolean getresult () {return check ();} Although JSP does not provide a label for accessing the business method, it provides a method for accessing attributes, therefore, this method indirectly calls the check business method by providing the result attribute. Modify the login_process.jsp file as follows: <% @ page contenttype = "text/html; charset = gb2312 "%> <% @ taglib prefix =" C "uri =" http://java.sun.com/jsp/jstl/core "%> <JSP: usebean id =" user "class =" JavaBean. user "Scope =" request "/> <JSP: setproperty name =" user "property =" * "/> <C: If test =" $ {user. result} "> <JSP: Include page =" success. JSP "/> </C: If> <C: If test =" $ {! User. Result} "> <JSP: Include page =" failure. jsp "/> </C: If>
Reference material: Basic tutorial on Java Web Programming