Application Server (AS) notes Application Server (AS) Notes
Application Server
a servlet/JSP container to run servlet and JSP web applications.  Application servers multiplex web clients to some shared database.
Web application
The Java Servlet specification defines a Web application as a collection of servlets, HTML pages, classes and other resources that are bundled and run on multiple containers from multiple vendors.

Servlets

Java Servlet
a platform-independent component that communicates with Web clients, using a request/response model, managed by a JSP/Servlet container.
To write a servlet, either the GenericServlet or HttpServlet class is extended.  The GenericServlet has an abstract service method that must be implemented.  The HttpServlet extends GenericServlet and requires you to implement one of these methods: doGet, doPost, doPut, doDelete, init, destroy or getServletInfo.  Typically, the doGet and doPost methods are implemented.

Servlets have the following life cycle:

The init method can be used to initialize servlet properties via the ServletConfig parameter.  Servlet initialization parameters are set in the web.xml file and passed to the init method in a ServletConfig parameter and can be accessed via the getInitParameter method.
The ServletContext can be thought of as a global storage area for a Web application.  Each Web application has a ServletContext.  Objects stored in the ServletContext persist for the life of the Web application, unless removed.

JSPs

Java server pages (JSP's) allow you to generate dynamic HTML pages by embedding Java logic into the requested document.  JSPs are a mix of HTML, special tags, and embedded Java code (like a view template).  Each JSP is translated from this mix into a servlet, used to send HTML back to the client, when the JSP is requested.

The generated Java code and the class file for this Servlet are stored in a specified location that defaults to the TOMCAT_HOME/work directory (see Tomcat, below); this directory can be specified in the Web application Context element.

Variables, implicitly available to a JSP, are:


The standard JSP tags are:

A sample application

Building a sample Web application that consists of a servlet, a bean and several JSP pages, the .\directory contains all JSP files (see Listing A).

JSPs

The index.jsp page uses the <jsp:forward> tag to effectively set the form.jsp as the home page for myapp.  The form.jsp page makes use of the <jsp:include> tag to add navigation and two separate forms to complete the form page.

Note how the third <jsp:include> tag uses the <jsp:param> tag to pass a parameter to the jsp_form.jsp page.  The servlet_form.jsp page is an example of using a servlet for form handling while the jsp_form.jsp page submits to another JSP page.

The company_name.jsp page uses the parameters submitted from the jsp_form.jsp page to build the colored company logo.  The bean_values.jsp page uses the <jsp:useBean> tag to store/retrieve/modify bean properties in the session and application scope.

The variables.jsp page shows examples of using some of the implicit variables available in a JSP.

Classes

The .\WEB-INF\classes\myapp directory contains the class files for the Servlet and bean used by the application (see in Listing B).
The MyBean class has one property called stringValue. The bean_values.jsp page is used to store an instance of this bean in the session and application scope.

The CompanyNameServlet is used as the destination of the form submission on the servlet_form.jsp page.  This servlet generates HTML output, based on parameters in the request.  Note that we've overridden the init method to set the companyName property, provided in the web.xml file.  It's common practice to have the doGet() or doPost() implementation in one method call the other implementation.

The .\WEB-INF this directory contains the web.xmlfile (see in Listing C), that defines the servlet and a context parameter.  Note the init-param, specifying the company name.  This parameter is extracted from the ServletConfig in the init method of the CompanyNameServlet.  The context parameter is accessed in the variables.jsp page.

To run the myapp application, place the files in the TOMCAT_HOME\webapps\myapp directory and start Tomcat.  Once Tomcat is running, type the URL http://localhost:8080/myapp into a browser.  This should bring up the index.jsp page with the form.jsp contents.

Tomcat

Tomcat is:

Configuration

Two environment variables, JAVA_HOME and TOMCAT_HOME, need to be specified. The JDK will install in JAVA_HOME directory; Tomcat 4 will install in TOMCAT_HOME.
Open a browser and enter the URL http://localhost:8080.  The default Tomcat webpage should appear.
Tomcat has a server.xml file, located in the TOMCAT_HOME\conf directory, for further customization, and offers a manager application and provides request-preprocessors called Valves and is able to implement filters.

Paths

On Tomcat, these resources are placed under the TOMCAT_HOME\webapps folder.  The directory structure for a sample Web application (myapp) would be: TOMCAT_HOME\webapps\application\WEB-INF\classes\lib

The application directory is considered the Web application root directory.  All JSP, HTML, JavaScript files and other resources are under this directory.  The WEB-INF directory contains resources, used by the application; but, WEB-INF is not in the public document root—no files contained in this directory structure are accessible by a client.

The classes directory (under WEB-INF) contains servlets, beans and utility classes, needed for the application’s operation.  If a class is present in a JAR file and in the classes directory, the class loader loads the one in the classes directory.

Back Home