JSP and JSTL Tutorials - Tutorial Addendum - Using JavaBean Classes

lifestyleappsios windows appcolt educationappsios
 31 December 18:00   

    



    



    

(Continued from antecedent part...)

    



    

It s actual absorbing to see the Servlet chic translated from UseBean.jsp:

    

 

    

package org.apache.jsp;

    

import javax. Servlet .*;

    

import javax. Servlet .http.*;

    

import javax. Servlet .jsp.*;

    

import org.apache.jasper.runtime.*;

    

import CacheBean;

    

public chic UseBean_jsp extends HttpJspBase {

    

clandestine changeless java.util.Vector _jspx_includes;

    

accessible java.util.List getIncludes() {

    

acknowledgment _jspx_includes;

    

}

    

accessible abandoned _jspService(HttpServletRequest request,

    

HttpServletResponse response)

    

throws java.io.IOException, ServletException {

    

JspFactory _jspxFactory = null;

    

javax. Servlet .jsp.PageContext pageContext = null;

    

HttpSession affair = null;

    

ServletContext appliance = null;

    

ServletConfig config = null;

    

JspWriter out = null;

    

Item page = this;

    

JspWriter _jspx_out = null;

    

try {

    

_jspxFactory = JspFactory.getDefaultFactory();

    

response.setContentType("text/xml;charset=UTF-8");

    

pageContext = _jspxFactory.getPageContext(this, request, response,

    

null, true, 8192, true);

    

appliance = pageContext.getServletContext();

    

config = pageContext.getServletConfig();

    

affair = pageContext.getSession();

    

out = pageContext.getOut();

    

_jspx_out = out;

    

out.write("<html>");

    

out.write("<body>");

    

CacheBean b = null;

    

synchronized (pageContext) {

    

b = (CacheBean) pageContext.getAttribute("b",

    

PageContext.PAGE_SCOPE);

    

if (b == null){

    

try {

    

b = (CacheBean) java.beans.Beans.instantiate(

    

this.getClass().getClassLoader(), "CacheBean");

    

} bolt (ClassNotFoundException exc) {

    

bandy new InstantiationException(exc.getMessage());

    

} bolt (Exception exc) {

    

bandy new ServletException("Cannot make bean of chic "

    

+ "CacheBean", exc);

    

}

    

pageContext.setAttribute("b", b, PageContext.PAGE_SCOPE);

    

}

    

}

    

JspRuntimeLibrary.introspecthelper(pageContext.findAttribute("b"),

    

"text", "Hello world!",null, null, false);

    

out.write("
Property from my Bean: ");

    

out.print(JspRuntimeLibrary.toString(((

    

(CacheBean)pageContext.findAttribute("b")).getText())));

    

out.write("<br/>");

    

out.write("
Info from my Bean: ");

    

out.print(b.getInfo());

    

out.write("</body>");

    

out.write("</html>");

    

} bolt (Throwable t) {

    

out = _jspx_out;

    

if (out != absent && out.getBufferSize() != 0)

    

out.clearBuffer();

    

if (pageContext != null) pageContext.handlePageException(t);

    

} assuredly {

    

if (_jspxFactory != null)

    

_jspxFactory.releasePageContext(pageContext);

    

}

    

}

    

}

    



    



    

Note that:

    



        

  • The "page" charge aspect with "import" aspect was translated into a true

        

    "import" Java statement.



  •     

  • The "useBean" activity aspect was translated into a block of cipher to amount bean

        

    class, instantiate an item of that bean class, and put that item into the

        

    attribute accumulating in the page ambience object. The name of the attribut is set

        

    to the aforementioned as the item name.



  •     

  • The "setProperty" activity aspect was translated into a alarm to introspecthelper()

        

    method.



  •     

  • The "getProperty" activity aspect was translated into a alarm to a "get" method.


  •     

  • The "expression" aspect was translated into a alarm to "out.print()".


  •     



    



    



    

Compilation Errors with Bobcat 4.1.18 and JDK 1.4.1

    



    

If you are using JDK 1.4.1 and aggravating to run the aloft example, you will get the

    

following accumulation error:

    

 

    

org.apache.jasper.JasperException: Clumsy to abridge chic for JSP

    

An absurdity occurred at line: 8 in the jsp file: /UseBean.jsp

    

Generated Servlet error:

    

[javac] Accumulation 1 antecedent file

    

C:localjakarta-tomcat-4.1.18workStandalonelocalhost

    

\_UseBean_jsp.java:7: . expected

    

import CacheBean;

    



    



    

This is because JDK 1.4.1 does not acquiesce acceptation account to be acclimated on classes

    

in bearding bales any more. For added detail information, amuse see area

    

"Tomcat 4.1.18 with JDK 1.4.1" in this book.

    



    

Setting and Accepting JavaBeans Properties

    



    

As you can see from antecedent sections, a JavaBean is just a approved Java obect.

    

Once a JavaBean is created in a JSP, you can use JSP setProperty and getProperty

    

action elements to set and retrieve ethics of its properties. To abutment these

    

action elements, a JavaBean chic haveto apparatus set and get methods based on some

    

rules.

    

(Continued on next part...)

    



    

 


Tags: action, class, write, catch, notes, public, tutorial, tutorials, response, object, print

 pagecontext, import, cachebean, write, class, translated, servlet, javabean, jspxfactory, element, object, action, javax, usebean, error, tomcat, response, apache, public, catch, print, , write <, translated into, javax servlet, element was, action element, jspx out, import javax, org apache, import javax servlet, org apache jasper, pagecontext page scope, notes using javabean, tutorials tutorial notes, tutorial notes using, jstl tutorials tutorial,

Share JSP and JSTL Tutorials - Tutorial Addendum - Using JavaBean Classes:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  jsp