Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
J2EE Tutorial - Java Bean 
 

To bring out the differences between the different appraoaches, we will consider a simple case where the user types his name in the textbox

 

J2EE Tutorial - Java Bean

                           

To bring out the differences between the different appraoaches,  we will consider a simple case where the user types his name in the textbox(text1) in the html form and submits it to the webserver. The JSP then invokes the greeter bean in the webserver . The greeter bean encapsulates the  'Business Logic' ,and so it  appends a greeting message to the name thus submitted and sends it back to the user.

What is a  Javabean?  A Javabean is just a java class  with the following requirements.

       i) It has a public no-args constructor

      ii) It has 'set' and 'get' methods for its properties.

      iii) It may have any general functions.

      iv) If required it must be Serializable.

     However,

      It is not necessary always that a bean should have properties.

     If there are no properties, we need not provide 'set' & 'get' methods either.

    ( Even the no-args constructor is provided by the compiler by default!)

    If  the bean uses library classes alone, it is automatically serializable.

    In that case, it becomes  just a class for encapsulating some functionality (ie) business logic.

 

   Such a bean , may or may not have a visual representation. As the field has  moved away from the desktop, to webserver, there is no point in providing a visual represenation for a component which works in server side as the user cannot interact with it directly. (Such a bean  used by jsp  in webserver, is exactly similar to ASP component). ( So  no worry about jar file ,BDK, BeanBox etc traditionally associated with JavaBean)

   Let us now create  such a bean. The Tomcat server searches for any classes in  c:\tomcat\webapps\root\web-inf\classes  folder. We create a subfolder under classes folder and name it 'ourbeans'.

   Therefore, we specify  the package information in the first line of our jspbean as  :

   package     ourbeans;

*************************

package ourbeans;

public  class greeterbean

{

    public greeterbean()   {    }

    public String  greetme(String s)

    {

    return   "How are you?...."+s;

    }

}

*****************************************************************

To compile the above file :

> javac greeterbean.java

We get greeterbean.class

We move this class file to:

  c:\tomcat\webapps\root\web-inf\classes\ourbeans

*****************************************************************

We will now create  greeter.htm & greeter.jsp and place them in:

c:\tomcat\webapps\root

greeter.htm

                <html>

                <body>

                               <form    method=post    action=greeter.jsp>

                               <input   type=text      name='text1'>

                               <input   type=submit>

                                </form>

                 </body>

                 </html>

greeter.jsp

                 <html>

                 <body>

          <jsp:useBean   id="greeter1"  class="ourbeans.greeterbean"    />

                              <%

                                     String   s = request.getParameter("text1");  

                                     String   a = greeter1.greetme(s);

                                     out.println(a);

                               %>

                  </body>

                 </html>

    In the above example, the business logic was invoked through an instance of 'greeter' bean class. So, it is object-oriented approach. Where was the method invoked? It was invoked in the Webserver.Where was the object available? The object was available in webserver itself.(ie) 'locally'.

This point is worth noting carefully. For  this reason, the jspbean method cannot be classified as 'Distributed Object ' Technology. Because, in Distributed Object Technology, the object on which the method is invoked locally, should be available in a remote server and not locally .( Why is this distinction so important? Is it important atall? If we are able to answer these questions  to ourselves convincingly, we have truly grasped the essence of Enterprise computing  but   not otherwise. That is why this point is being laboured.

                           

» View all related tutorials
Related Tags: java c j2ee ant language io tutorial experience ria this languages programmer student for ie program to ram presentation programmers

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.