Define a JavaBean (JavaBean is actually a simple java class)
Javac-d./UserBean. java generate package file
01
Package com. javaweb. ch07;
02
// A simple JavaBean example
03
Public class UserBean {
04
// User name attributes
05
Private String username;
06
// User Password attributes
07
Private String password;
08
// Obtain the user name
09
Public String getUsername (){
10
Return username;
11
}
12
// Set the user name
13
Public void setUsername (String username ){
14
This. username = username;
15
}
16
// Obtain the User Password
17
Public String getPassword (){
18
Return password;
19
}
20
// Set the user's password
21
Public void setPassword (String password ){
22
This. password = password;
23
}
24
}
01
<% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
02
<! DOCTYPE html>
03
<Html>
04
<Head>
05
<Title> call JavaBean </title>
06
</Head>
07
<Body>
08
<% -- Call JavaBean through the JavaBean Action Command -- %>
09
<Jsp: useBean id = "user" scope = "page" class = "com. javaweb. ch07.UserBean"> </jsp: useBean>
10
<%
11
// Set the username attribute of the user
12
User. setUsername ("zhangdapeng ");
13
// Set the password attribute of the user
14
User. setPassword ("zhangda890126 ;;");
15
// Print the username of the output user
16
Out. println ("username:" + user. getUsername () + "<br/> ");
17
// Print the output user password
18
Out. println ("user Password:" + user. getPassword () + "<br/> ");
19
%>
20
<%
21
Try {
22
Class. forName ("com. mysql. jdbc. Driver"); // load the JDBC Driver
23
} Catch (ClassNotFoundException e ){
24
Out. println ("unable to find the driver class"); // when an exception is thrown, a message is displayed.
25
}
26
%>
27
</Body>
28
</Html>
The generated class must be placed under the WEB-INF's classes folder under the directory of your project, if not, create your own
From Zhang Dapeng's blog