For the Usage Analysis of JavaBean in JSP: We can use the scriptlet method: <%! %>/<%>
You can also use the <JSP: usebean> method // bean. hellobean. Java:
package bean;public class HelloBean {private String str;public void setStr(String s){str = s;}public String getStr(){return str;}}
//index.jsp
<%@page import="bean.HelloBean"%>
Meanings of JSP: usebean parameters: ID: object instance name scope: Scope of bean function,
The default value is page, which is valid for the entire JSP page.
Meaning of various scope parameters: page: only covers pages using JavaBean (pagebean. jsp/counterbean. Java)
Request: The valid range is limited to requests that Use Javabean (requestbean. jsp/requestbean2.jsp/counterbean. Java)
Session: the valid range is in the user's entire connection process (all sessions are valid) (sessionbean. jsp/session2.jsp/counterbean. Java)
Application: the effective scope covers the entire application. That is, it is valid for the entire web application (application. jsp/application2.jsp/counterbean. Java)
// Go to the Tomcat work directory to view the compiled Java file of JSP:
/** Generated by the Jasper component of Apache Tomcat * version: Apache Tomcat/7.0.22 * generated at: 2011-11-02 01:36:52 UTC * Note: the last modified time of this file was set to * The Last Modified Time of the source file after * generation to assist with modification tracking. */package Org. apache. JSP; import javax. servlet. *; import javax. servlet. HTTP. *; import javax. servlet. JSP. *; import bean. he Lobean; public final class index_jsp extends Org. apache. jasper. runtime. httpjspbase implements Org. apache. jasper. runtime. jspsourcedependent {bean. hellobean membean = new bean. hellobean (); // <%! Bean. hellobean membean = new bean. hellobean (); %> <%! %> Make the bean variable the member variable of this class Private Static final javax. servlet. JSP. jspfactory _ jspxfactory = javax. servlet. JSP. jspfactory. getdefafactory Factory (); Private Static Java. util. map <Java. lang. string, Java. lang. long> _ jspx_dependants; private javax. el. expressionfactory _ el_expressionfactory; private Org. apache. tomcat. instancemanager _ jsp_instancemanager; Public Java. util. map <Java. lang. string, Java. lang. long> getdepen Dants () {return _ jspx_dependants;} public void _ jspinit () {_ el_expressionfactory = _ jspxfactory. getjspapplicationcontext (getservletconfig (). getservletcontext ()). getexpressionfactory (); _ jsp_instancemanager = org. apache. jasper. runtime. instancemanagerfactory. getinstancemanager (getservletconfig ();} public void _ jspdestroy () {} public void _ jspservice (final javax. servlet. HTTP. httpservletrequest Request, final javax. servlet. HTTP. httpservletresponse response) throws Java. io. ioexception, javax. servlet. servletexception {final javax. servlet. JSP. pagecontext; javax. servlet. HTTP. httpsession session = NULL; Final javax. servlet. servletcontext application; Final javax. servlet. servletconfig config; javax. servlet. JSP. jspwriter out = NULL; Final Java. lang. object page = This; javax. servlet. JSP. jspwriter _ jspx_out = NULL; javax. servlet. JSP. pagecontext _ jspx_page_context = NULL; try {response. setcontenttype ("text/html"); pagecontext = _ jspxfactory. getpagecontext (this, request, response, null, true, 8192, true); _ jspx_page_context = pagecontext; application = pagecontext. getservletcontext (); Config = pagecontext. getservletconfig (); Session = pagecontext. getsession (); out = pagecontex T. getout (); _ jspx_out = out; out. write ("\ r \ n"); out. write ("<HTML> \ r \ n"); out. write ("<body> \ r \ n"); out. write ("\ r \ n"); out. write (""); out. write ("\ r \ n"); out. write (""); membean. setstr ("membean !! "); Out. write ("\ r \ n"); out. write (""); out. print (membean. getstr (); out. write ("\ r \ n"); out. write ("\ r \ n"); out. write (""); bean. hellobean partbean = new bean. hellobean (); // <% bean. hellobean partbean = new bean. hellobean () ;%><%%> make bean a local variable out of the member function of the class. write ("\ r \ n"); out. write (""); partbean. setstr ("partbean !! "); Out. write ("\ r \ n"); out. write (""); out. print (partbean. getstr (); out. write ("\ r \ n"); out. write ("\ r \ n"); out. write (""); bean. hellobean pagebean = NULL; // <JSP: usebean id = "pagebean" Scope = "page" class = "bean. hellobean "> pagebean = (bean. hellobean) _ jspx_page_context.getattribute ("pagebean", javax. servlet. JSP. pagecontext. page_scope); If (pagebean = NULL) {pagebean = new bean. hellobean (); _ jspx_pa Ge_context.setattribute ("pagebean", pagebean, javax. servlet. JSP. pagecontext. page_scope); out. write ("\ r \ n"); out. write ("");} Out. write ("\ r \ n"); out. write ("\ r \ n"); out. write (""); pagebean. setstr ("pagebean !! "); Out. write ("\ r \ n"); out. write (""); out. print (pagebean. getstr (); out. write ("\ r \ n"); out. write ("\ r \ n"); out. write ("\ t"); bean. hellobean requestbean = NULL; // <JSP: usebean id = "requestbean" Scope = "request" class = "bean. hellobean "> requestbean = (bean. hellobean) _ jspx_page_context.getattribute ("requestbean", javax. servlet. JSP. pagecontext. request_scope); If (requestbean = NULL) {requestbean = new Bean. hellobean (); _ jspx_page_context.setattribute ("requestbean", requestbean, javax. servlet. JSP. pagecontext. request_scope); out. write ("\ r \ n"); out. write ("");} Out. write ("\ r \ n"); out. write ("\ r \ n"); out. write (""); requestbean. setstr ("pagebean !! "); Out. write ("\ r \ n"); out. write (""); out. print (pagebean. getstr (); out. write ("\ r \ n"); out. write ("\ r \ n"); out. write (""); bean. hellobean sessionbean = NULL; // <JSP: usebean id = "sessionbean" Scope = "session" class = "bean. hellobean "> synchronized (Session) {sessionbean = (bean. hellobean) _ jspx_page_context.getattribute ("sessionbean", javax. servlet. JSP. pagecontext. session_scope); If (sessionbean = nu Ll) {sessionbean = new bean. hellobean (); _ jspx_page_context.setattribute ("sessionbean", sessionbean, javax. servlet. JSP. pagecontext. session_scope); out. write ("\ r \ n"); out. write ("") ;}} out. write ("\ r \ n"); out. write ("\ r \ n"); out. write (""); sessionbean. setstr ("pagebean !! "); Out. write ("\ r \ n"); out. write (""); out. print (sessionbean. getstr (); out. write ("\ r \ n"); out. write ("\ r \ n"); out. write (""); bean. hellobean appbean = NULL; // <JSP: usebean id = "appbean" Scope = "application" class = "bean. hellobean "> synchronized (Application) {appbean = (bean. hellobean) _ jspx_page_context.getattribute ("appbean", javax. servlet. JSP. pagecontext. application_scope); If (appbean = NULL ){ Appbean = new bean. hellobean (); _ jspx_page_context.setattribute ("appbean", appbean, javax. servlet. JSP. pagecontext. application_scope); out. write ("\ r \ n"); out. write ("") ;}} out. write ("\ r \ n"); out. write ("\ r \ n"); out. write (""); sessionbean. setstr ("appbean !! "); Out. write ("\ r \ n"); out. write (""); out. print (sessionbean. getstr (); out. write ("\ r \ n"); out. write ("\ r \ n"); out. write ("</body> \ r \ n"); out. write ("