JavaWeb/ JavaBean

First, what is a JavaBean?

JavaBean in essence, is a special Java class, it must comply with the four-point guidelines: 1, must be a public class. 2, must contain a constructor with no arguments. 3 ,. for variables which must have a Get, Set method. 4, all properties must be private.

It can be said, JavaBean is a Java class, just to comply with some special conventions only.

 

Second, why should we use JavaBean?

Technology must be based on the birth, the birth of some demand from the JavaBean is no exception. JSP technology allows us to write HTML and JAVA program fused together, but there have been confusing code structure issues, a lot of JAVA code mixed with HTML code, not only makes repetitive code much higher (for example, in different position to write the same functionality as the need to write code that many times the same), and when the error is not easy to find out exactly what went wrong. To solve this problem, I was born JavaBean.

The idea is to JavaBean Java code package, all of the properties are privately owned, controlled only by the get / set methods. This requires the use of a function of time, you can directly call his class, thereby reducing duplication of code.

 

Three, JavaBean Demo:

 1 package JAVAbean;
 2 
 3  
 4 
 5 public class JavaBeanDemo {
 6 
 7     private String user;
 8 
 9     private String password;
10 
11     public JavaBeanDemo(){}
12 
13     public String getUser() {
14 
15         return user;
16 
17     }
18 
19  
20 
21     public String getPassword() {
22 
23         return password;
24 
25     }
26 
27  
28 
29     public void setUser(String user) {
30 
31         this.user = user;
32 
33     }
34 
35  
36 
37     public void setPassword(String password) {
38 
39         this.password = password;
40 
41     }
42 
43 }

 

 

Four, JavaBean to call in the jsp inside.

JavaBean in JSP is invoked by the JSP tag action.

<Jsp: useBean id = "Bean name in the current jsp in" class = "JavaBean name (to have the package name) scope =" bean effective range "" \>

Scope has requeset, page, application, session four. Determine the bean life cycle.

 

 

Five, demo

Simple to use

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 
 3 <html>
 4 
 5 <head>
 6 
 7     <title>Title</title>
 8 
 9 </head>
10 
11 <body>
12 
13     <jsp:useBean id="login" class="JAVAbean.JavaBeanDemo" scope="page"/>
14 
15     <%
16 
17         login.setUser("1234");
18 
19         login.setPassword("1234");
20 
21     %>
22 
23     用户名<%=login.getUser()%><br>
24 
25     密码<%=login.getPassword()%>
26 
27  
28 
29 </body>
30 
31 </html>

 

Six, scope: Life Cycle

 

Requeset: Use <jsp: forwoard> or with <jsp: include> jsp operation of introducing new is the object request to be transmitted to the next may define a page, the next page can be used also passed object.

Page: valid only in the current page.

Application: shut down the server can act to exist.

Session: only valid in the current build session.

 

 

End: Moe initial learning new dishes of chicken, there must be many mistakes get points out, also please correct me a lot Gangster

Guess you like

Origin www.cnblogs.com/wu199723/p/11573976.html