execution of components in containers

Hi everyone,

i've just started to learn about j2ee and i have some questions. First of all i don't get the notion of containers. If i get it right you have containers and components run in containers. For example you have a servlet which runs in a web container. But what does it mean to 'run' in something. More specifically, what is the role of containers in the execution of for example a servlet or a session bean. I understand that j2ee builds upon j2se , so i guess that all the concepts of j2ee should somehow be mapped to regular java code that runs on jvm. But how does this happen?

Second of all i don't get why we need WAR files?

Any help would be highly appreciated.

[709 byte] By [jamesvha] at [2007-10-2 8:36:38]
# 1

The J2EE specification (mainly for Servlet) defines life-cycle events for a given implementation of javax.servlet.Servlet. In particular, there are init() and destroy() methods. These will be invoked by the container itself at startup and shutdown (and in certain rarer circumstances depending on container configuration). So, what is really going on?

Well, the container is expecting an instance of javax.servlet.Servlet to service requests. The container itself is an application that has a normal main() method. From this standpoint, you are still J2SE. However, the J2EE specification, rather than having you use main() everywhere in your Servlets, instead provides events like init(), service() and destroy(). The container is fundamentally responsible for managing the scope and life-cycle of a given Servlet.

The container 'locates' the correct Servlet to execute via the web.xml file. (Sometimes also additional configuration files like server.xml in Tomcat). These are specified via url-mapping (which will let the container know which Servlet name to execute) and the servlet-class tags (which let the container know which Java class to create via reflection). Once a given javax.servlet.Servlet is instantiated, the container can execute one of the methods provided in the interface, normally service().

- Saish

Saisha at 2007-7-16 22:38:13 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

As for WAR files, they are not strictly necessary. You can either deploy as a WAR or as an 'exploded' directory structure (e.g., simply leave the web application folder structure as is rather than compressing). The WAR format has the advantage of allowing you to deploy easier, especially if you go full blown EJB and create an EAR deployment.

The web application structure will typically look like:

root

css

html

js

jsp

src

WEB-INF/classes

WEB-INF/lib

WEB-INF/web.xml

xml

The WEB-INF folder has a special status within a J2EE application. Classes will automatically be loaded fro WEB-INF/classes and from within any JAR files in WEB-INF/lib. The container also expects a web.xml file within WEB-INF. The remaining folder locations are up to you. The above is simply a typical layout that I use.

One final note: you should be able to locate any resources in your application (except those in WEB-INF, unless you add those folders to your CLASSPATH when running your container) via:

ServletContext.getResourceAsString(name);

This is normally easier than trying to hard-code a location to fetch via FileInputStream.

- Saish

Saisha at 2007-7-16 22:38:13 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...