Jeff,
Let me see if I can clarify the servlets versus beans issue:
  • A Servlet is essentially a "plug-in" to extend the capabilities of a web server. The basic definition of a servlet is any class that implements the javax.servlet.Servlet interface. This interface allows a web server to load this class as a plug in and send HTTP requests directly to the servlet via whatever URL the servlet is registered under.
  • A Java bean is actually a somewhat loose concept, which one of the strengths of the bean definition, but seems to cause a lot of confusion with people first learning about them. A bean is essentially a reusable component written in Java. To be a bean, a Java class does not need to extend any particular class, or even implement a particular interface. Instead, a bean is just a Java class that follows certain conventions that let it be used building-block environment, where components from different sources are plugged together to build an app. A bean class must have a public constructor with no arguments. If the bean wants to expose "properties" to the build environment, it needs to define get/set methods for those properties; if it wants to inter-operate with other beans, it needs to define get/set event listener methods; if it wants to allow build-time configuration it needs to define property editors; and so on.
Beans can be used in any number of environments. They were actually were developed defining reusable UI components, so many descriptions of beans seem to assume that they do visual display, but this is not at all necessarily the case.

The relevance for beans to JSP is that they provide a way to define reusable components in Java that can be assembled into a Web app.

It is important to remember that every JSP page is actually a servlet; it gets compiled into a Java class that implements javax.servlet.Servlet. Therefore, pretty much anything that is true of a servlet is true of a JSP page. In addition, any servlet can itself be considered a bean, as long as it follows the bean conventions.

So, to address your questions:

  • Servlets execute in the web server (or a servlet engine linked to the web server)
  • Beans can execute anywhere, but when they are used in conjunction with JSP and servlets, they execute in the servlet engine, also
  • Servlets can be directly invoked from a web browser, using the URL the web server has mapped them to (for instance "http://myhost.com/servlets/myservlet")
  • Beans can not be directly invoked from a web browser unless they are also a servlet (ie. they implement the Servlet interface)
  • Beans can be called from servlets (and JSP files) as long as the bean class can be found (and loaded) by the servlet engine
  • Server side beans do *not* need to do visual display. As a matter of fact, if they are running in a web server environment, it would generally be a bad thing if they did.
I hope this helps and doesn't confuse you any further. I'm sure other people on this list will correct any misstatements I have made.

Yours, JonTom

    JonTom Kittredge
    ITA Software
    Cambridge, Massachusetts

"Hu, Jeffery X (Jeff)" wrote:

 

Hi, Java gurus:

Can anyone tell me the differences between <% @include file="pathToFile" %> and <jsp:include page="pathtofile" />.  Giving examples will be good.

As well, I'd like to clearify some concepts - servlet and bean.  Both servlet and bean reside in the web server.    Servlets can be directly requested by clients(from browser) but beans can not.    Beans can be initiated by servlets or JSP files.    The server side beans do need visual presentations.  Correct me if I am wrong.

Thanks in advance
 

Reply via email to