Jsp and javaBean --- simple use of javaBean

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.