JSP Tutorial: How to Use JavaBean to complete business logic, jspjavabean
This example describes how to Use JavaBean to complete business logic in the JSP tutorial. Share it with you for your reference. The details are as follows:
I. Objectives:
① Understand what is JavaBean;
② Master how to write JavaBean;
③ Master how to access JavaBean in JSP;
④ Understand the Four Scopes of objects in JSP.
Ii. Main content:
Encapsulate the verification process in login_process.jsp in the previous lecture to form a JavaBean. Then, call this JavaBean on the JSP page to introduce the compilation and access of JavaBean.
A simple example is provided to introduce the four scopes of the JavaBean object.
1. What is JavaBean?
JavaBean is a component written in Java. 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 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, the call situation 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
Copy codeThe Code is as follows: <jsp: useBean id = "" class = "" scope = ""/>
Purpose: instantiate the JavaBean object or 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 application
Page indicates the current page, which is actually the Java class object corresponding to the current JSP file;
Request indicates a request, which starts from when we send the request to when the system responds to us. 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 ):
Compile the page. jsp file 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="applicationAttribute" value="aaaa" scope="application"/><jsp:include page="success.jsp"/>
Output Information in page. jsp:
${pageScope.pageAttribute}${requestScope.requestAttribute}${sessionScope.sessionAttribute}${applicationScope.applicationAttribute}
The <c: set> label 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.
Compile the request. jsp file as follows:
<%@ page contentType="text/html;charset=gb2312"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Output Information in request. jsp:
$ {Pagination. pageAttribute }$ {requestScope. requestAttribute }$ {sessionScope. sessionAttribute }$ {applicationScope. applicationAttribute} <br> <a href = "session. jsp "> Access session </a>
Compile the session. jsp file as follows:
<%@ page contentType="text/html;charset=gb2312"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Session. jsp output information:
${pageScope.pageAttribute}${requestScope.requestAttribute}${sessionScope.sessionAttribute}${applicationScope.applicationAttribute}
Compile the application. jsp file as follows:
<%@ page contentType="text/html;charset=gb2312"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Output Information in application. jsp:
${pageScope.pageAttribute}${requestScope.requestAttribute}${sessionScope.sessionAttribute}${applicationScope.applicationAttribute}
Running process:
Run page. jsp and get the following result:
Output Information in request. jsp: rrrr ssss aaaa
Output Information in page. jsp: pppp rrrr ssss aaaa
Analysis: Because page. jsp and request. jsp belong to the same request, the request object, session object, and application object are shared, but the information stored in the page cannot be displayed.
Click "Access session" for session. jsp
Session. jsp output: ssss aaaa
Analysis: This is not the same as the previous request. Therefore, you cannot access the request and page information. You can only access session and application.
Open a new browser and access application. jsp.
Output Information in application. jsp: aaaa
Analysis: It belongs to different clients, so only application objects can be shared and only information in application can be seen.
2) assign values to attributes
<Jsp: setProperty name = "" property = "" value = "/>
Name indicates the name of the JavaBean object, and property indicates the attribute name and the value to be assigned.
Example: <jsp: setProperty name = "user" property = "username" value = "zhangsan"/>
If the value attribute is not specified, 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, and the property attribute is set to "*", it is equivalent:
String username = request.getParameter("username");user.setUsername(username);String userpass = request.getParameter("usernpass");user.setUsernpass (userpass);
If the name of a form element is inconsistent with 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
Copy codeThe Code is as follows: <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: compiled Java file User. 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: rebuilding login_process.jsp
Add the following method to User. java:
public boolean getResult(){ return check();}
Although JSP does not provide a label for access to business methods, it also provides access to properties. Therefore, this method indirectly calls the check business method by providing the result property.
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>
I hope this article will help you with JSP program design.