| Refresh | Home EGTry.com

jsp custom example, useBean


useBean jsp usage



<jsp:useBean id="obj" class="myservlet.MyBean" type="DesiredType" scope="page">
	this is the body
	<jsp:setProperty name="obj" property="age" value="37"/>
</jsp:userBean>



generated java code


DesiredType obj=null;
boolean obj_created=false;

	// instantiate the javabean object, or get the javabean object
	synchronized(pageContext) { // scope="page", other objects may be: request, session and application
		obj=(DesiredType)pageContext.getAttribute("obj", PageContext.PAGE_SCOPE);
		if (obj==null) {
		obj_created=true; //??? why set this as true before instantiate the object?
		obj=(myservlet.MyBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(),
						"myservlet.Mybean");
		pageContext.setAttribute("obj", obj, PageContext.PAGE_SCOPE);
	}

	// only the instatiation was successful, we execution the body
	if (obj_created == true) {
		out.write("this is the body"); // this is the body
		JspRuntimeLibrary.introspecthelper(pageContext.findAttribute("obj"), "age", "37", null, null, false);
		...
	}