Define a JavaBean (JavaBean is actually a simple Java class)
Build Package file
Package com.javaweb.ch07;
A simple JavaBean example public
class userbean{
//username attribute
private String username;
User Password attribute
private String password;
Get user name public
String GetUserName () {return
username;
}
Set user name public
void Setusername (String username) {
this.username = username;
}
Get user Password public
String GetPassword () {return
password;
}
Set the user's password public
void SetPassword (String password) {
this.password = password;
}
}
<% @page language= "java" contenttype= "text/html;charset=gb2312"%>
<! DOCTYPE html>
The generated class must be placed under the Classes folder in the Web-inf in the directory of your own project, not created yourself.
To set JavaBean properties:
use <jsp:setProperty> action directives in JSP to set JavaBean properties, there are four different formats
<jsp:setproperty name= "Instantiate object name" property= "*"/>
The "*" in this way means that the JavaBean property is set based on all the parameters of the table conveys, and the parameter value passed in must be consistent with the name of the property in JavaBean
<% @page language= "java" contenttype= "text/html;charset=gb2312"%> <% @page import= "java.sql.*"%> <! DOCTYPE html>
Submit a form page ditto
<% @page language= "java" contenttype= "text/html;charset=gb2312"%> <!
DOCTYPE html>
Note Here password the Name property of the form
<% @page language= "java" contenttype= "text/html;charset=gb2312"%>
<! DOCTYPE html>
The code above notices the blue part of the red, which is a better elasticity.
<jsp:setproperty name= "instantiated object Name" property= "Property name value=" property value "/>
<% @page language=" java "contenttype=" text /html;charset=gb2312 "%>
<! DOCTYPE html>
To get JavaBean properties:
<jsp:getproperty name= "property=" "/>
<% @page language=" java "contenttype=" text/html;charset=gb2312 " %>
<! DOCTYPE html>
Set the scope of the JavaBean:
Set page-range JavaBean
<% @page language= "java" contenttype= "text/html;charset=gb2312"%> <! DOCTYPE html>
Set the JavaBean for the request scope
<% @page language= "java" contenttype= "text/html;charset=gb2312"%> <! DOCTYPE html>
Set JavaBean for session scope
<% @page language= "java" contenttype= "text/html;charset=gb2312"%> <! DOCTYPE html>
Set the application range JavaBean
<% @page language= "java" contenttype= "text/html;charset=gb2312"%> <! DOCTYPE html>
To remove JavaBean properties:
the removal of JavaBean varies according to different ranges
Page range, request range, session range, application range corresponding to use Pagecontext,request,session,application
Method uses the RemoveAttribute () method
Example
<% @page language= "java" contenttype= "text/html;charset=gb2312"%>
<! DOCTYPE html>