Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
EJB
  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(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.

                           

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.