Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I write a J2EE application?

Status
Not open for further replies.

aplusc

Programmer
Feb 17, 2002
44
US
I am very new to J2EE and just trying to figure out how to do things.

I built a simple application - a JSP to display data and a servlet to get that data from a database. It seems to work well, but from what I understand it would make sense to make the servlet into a Java Bean instead.

But what do I do after that? How would I call my Bean's methods from the servlet or JSP? And most importantly, how do I get my Bean to run on the server? I am using Apache Tomcat 4.1.

Thanks in advance ...
 
It would not make any sense to 'make' a servlet into bean - as they perform very different tasks.

Beans are just regular java classes, but written in a certain way, so that things like JSP and servlets can understand and access their methods. Simple example :

Code:
public class TestBean {

   private String name;

   //a getter method 
   public String getName() {
       return name;
   }

   //a setter method
   public void setName() {
       this.name = name;
   }
}

Thus, a JSP could access the data in this bean by using the method calls 'useBean', ad then setting and retrieving info via the setproperty and getProperty.

The servlet is used as the Contoller, and binds the bean to a name which the JSP can use using the setAttribute method.

The bean does you business logic or Model, and your JSP is the View. This is the classic MVC method of application design in J2EE.

The bean is placed in the same dir as your servlet.

I suggest looking up MVC on the sun web site, and doing some of the tutorials there, or buy a book like Servlet Programming or JavaServer Pages, both published by O'Reilly .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top