Sun Java System Web Server 7.0 Update 4 Developer's Guide to Java Web Applications

Chapter 2 Web Applications Overview

This chapter provides a basic overview of how web applications are supported in Web Server. This chapter includes the following sections:

Java Web Applications

Web Server supports the Java™ Servlet 2.5 API specification and the JavaServer Pages™ (JSP™) 2.1 specification, which allows servlets and JSPs to be included in web applications.

A web application is a collection of servlets, JavaServer Pages, HTML documents, and other web resources that include image files, compressed archives, and other data. A web application can be packaged into a web archive (WAR) file or exist in an open directory structure.

Web Server 7.0 also supports, SHTML and CGI, which are not Java Platform, Enterprise Edition (Java EE) application components. For more information about APIs and programming technologies, see Sun Java System Web Server 7.0 Update 4 Developer’s Guide.

Developing and Deploying Web Applications

This section describes how to create, secure, and deploy web applications using Web Server.

Creating Web Applications

This section lists the general actions to create a web application, and provides pointers to more information.

  1. Create a directory for all of the web application's files. This directory is the web application's document root.

  2. Create any needed HTML files, image files, and other static content.

  3. Place these files in the document root directory or a subdirectory where they can be accessed by other parts of the application.

  4. Create any needed JSP files.

    For more information, see Chapter 5, Developing JavaServer Pages

  5. Create any needed servlets.

    For more information, see Chapter 4, Developing Servlets.

  6. Compile the servlets.

    For details about precompiling JSPs, see Compiling JSPs Using the Command-Line Compiler.

  7. Organize the web application as described in Web Application Structure.

  8. Create the deployment descriptor files.

    For more information, see Creating Web Deployment Descriptors.

  9. (Optional) Package the web application in a .war file. For example:

    jar -cvf module_name.war *.

  10. Deploy the web application. For more information, see Deploying Web Applications.

    You can create a web application manually or you can use Java System Enterprise Studio. For more information about developing web applications in Sun Java Enterprise Studio, see http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/helloweb.html

About Securing Web Applications

You can write secure web applications for the Sun Java System Web Server with components that perform user authentication and access authorization. You can build security into web applications using the following mechanisms:

For detailed information about these mechanisms, see Chapter 8, Securing Web Applications.

About Deploying Applications

A web application is a collection of servlets, JSP, HTML documents, and other web resources that might include image files, compressed archives, and other data. A web application can be packaged into a Web ARchive file (a WAR file) or exist in an open directory structure. For more information, see Chapter 9, Deploying Web Applications.

About Virtual Servers

A virtual server is a virtual web server that uses a unique combination of IP address, port number, and host name to identify it. You might have several virtual servers all of which use the same IP address and port number but are distinguished by their unique host names.

When you first install Web Server, a default virtual server is created. You can also assign a default virtual server to each new HTTP listener you create. For details, see Sun Java System Web Server 7.0 Update 4 Administrator’s Guide.

About Default Web Applications

A web application that is deployed in a virtual server at a URL / becomes the default web application for that virtual server. To access the default web application for a virtual server, type the URL for the virtual server but do not supply a context root. For example:

http://myvirtualserver:3184/

If none of the web applications under a virtual server are deployed at the URI ”/”, the virtual server serves HTML or JSP content from its document root, which is usually instance_dir/docs. To access this HTML or JSP content, point your browser to the URL for the virtual server specify the target file rather than a context root. For example:

http://myvirtualserver:3184/hellothere.jsp

Servlet Result Caching

The Web Server can cache servlet or JSP results to make subsequent calls to the same servlet or JSP page faster.

For more information about response caching as it pertains to servlets, see Caching Servlet Results.

JSP Cache Tags

JSP cache tags enable you to cache JSP page fragments within the Java engine. Each can be cached using different cache criteria. For example, if you have page fragments to view stock quotes and weather information, you can cache the stock quote fragment for 15 minutes, and the weather report fragment for 25 minutes.

For more information about JSP caching, see JSP Cache Tags.

Database Connection Pooling

Database connection pooling enhances the performance of servlet or JSP database interactions. For more information about the JavaTM DataBase Connectivity (JDBCTM) software, see Configuring JDBC Resources in Sun Java System Web Server 7.0 Update 4 Administrator’s Guide.

The simplest connection pool can be configured by using the given. In this example, the connection pool is assigned to use the ORACLE JDBC driver.

ProcedureTo Configure a Simple Connection Pool

In this example procedure, the connection pool uses the ORACLE JDBC driver.

  1. Start wadm


    $ ./bin/wadm --user=admin
    Please enter admin-user-password>user-admin-password
    Sun Java System Web Server 7.0 B01/02/2006 14:22
    wadm>
  2. Verify the list of available configurations.


    wadm>list-configs
    
  3. Create the jdbc-resource configuration.

    For more information about all possible elements, see Sun Java System Web Server 7.0 Administrator's Configuration File Reference.


    wadm>create-jdbc-resource --config=test 
    --datasource-class=oracle.jdbc.pool.OracleDataSource jdbc/MyPool
    
  4. Add properties to jdbc-resource.

    Properties are primarily used to configure the driver's vendor-specific properties. In the following example, the values for the properties url, user, and password are added to jdbc-resource.


    wadm>list-jdbc-resource-userprops --config=test --jndi-name=jdbc/MyPool 
    password=mypassword user=myuser url=jdbcZ:oracle:thin:@hostname:1421MYSID
    
  5. Enable the connection validation.


    wadm>set-jdbc-resource-prop --config=test --jndi-name=jdbc/MyPool
    connection-validation-table-name=test connection-validation=table
    CLI201 Command "set-jdbc-resource-prop" ran successfully
  6. Change the pool setting.

    In this example, the maximum number of connections is set to 100.


    wadm>set-jdbc-resource-prop --config=test --jndi-name=jdbc/MyPool max-connections=100
    CLI201 Command "set-jdbc-resource-prop" ran successfully.
  7. Deploy the configuration.


    wadm>deploy-config test
    CLI201 Command "deploy-config" ran successfully.

    You can install a JDBC driver with Java Archive (JAR) files in one of the following ways:

    • Copy the driver's JAR file into the server's instance library directory. JAR files in the instance library directory will automatically load and available for server.

    • Modify the JVM class-path- suffix to include JDBC drivers jar file. The new value will overwrite the old value of the element. For example, wadm> set-jvm-prop --config=test class-path-suffix=/export/home/lib/classes12.jar

Sample Applications in Web Server 7.0

Sun Java System Web Server 7.0 includes a set of sample applications, which can be found in the install-dir/samples directory. All the sample applications are arranged in a specific and well-defined directory structure. The sample application included as part of the Sun Java System Web Server are packaged into consistent directory structure as elaborated below:

Sample Directories

All of the sample applications are arranged in a specific and well-defined directory structure. In general, the top-level directory of a sample application includes the following:

Java Samples

A complete index of Java sample applications provided as part of the Sun Java System Web Server product is listed below:

Table 2–1 Java Samples

Directory 

Contents 

apis

This directory contains source code of the extensible session manager and session store API that was published in the previous release of Web Server (6.0). These APIs have been deprecated since Sun ONE Web Server 6.1 and are provided in this directory for the purpose of backward compatibility. 

Directory path: install-dir/samples/java/apis

simple

Basic JSP and servlet examples combined into a single web application (Tomcat samples).  

Directory path: install-dir/samples/java/webapps/simple

caching

JSP and servlet examples that demonstrate how to cache results of JSP and servlet execution. 

Directory path: install-dir/samples/java/webapps/caching

jstl

Basic examples that demonstrate usage of the JSP Standard Tag Library. 

Directory path: install-dir/samples/java/webapps/jstl

rmi-iiop

Basic example that demonstrates using a servlet to access a stateless EJBTM using RMI/IIOP running in Sun Java System Application Server 8.x.

Directory path: install-dir/samples/java/webapps/rmi-iiop

javamail

A servlet that uses the Javamail API to send an email message. 

Directory path: install-dir/samples/java/webapps/javamail

i18n

A basic Java EE web application that demonstrates how to dynamically change the display language based on user preference. 

Directory path: install-dir/samples/java/webapps/i18n

JSF

Example showing how to use Java Server Faces (JSF) components to quickly build web application user interfaces. JSF examples are included in the following directories: 

  • carstore: Online car store sample

  • components: JSF custom components sample

  • guessnumber: Guess number sample

Directory path: install-dir/samples/java/webapps/jsf

jdbc

Java DataBase Connectivity examples are included in the following directories:  

  • blob: A servlet that accesses BLOBs through the JDBC API

  • simple: A basic servlet that accesses an RDBMS through the JDBC API

  • transactions: A servlet that uses the transaction API with JDBC to control a local transaction

  • rowset: Demonstrates the JDBC rowset feature

Directory path: install-dir/samples/java/webapps/jdbc

jndi

Java Naming and Directory Interface examples are in the following directories: 

  • custom: Demonstrates how to use the custom resource

  • external: Demonstrates how to use the external resource

  • readenv: Demonstrates how to use the environment entries specified in the web.xml file

  • url: Demonstrates how to use the servlet with the URL resource facility to access a resource

Directory path: install-dir/samples/java/webapps/jndi

security

Examples demonstrating how to secure Java EE web applications through standard authentication mechanisms and access controls. Security examples are included in the following directories: 

  • basic-auth: Demonstrates how to develop, configure, and exercise basic authentication

  • client-cert: Demonstrates how to develop, configure, and exercise client certificate authentication

  • form-auth: Demonstrates how to develop, configure, and exercise form-based authentication

  • jdbcrealm: Demonstrates how to develop, configure, and exercise JDBC realm authentication

Directory path: install-dir/samples/java/webapps/security

webservices/security

Web services security examples are located in the following directory: 

  • fromwsdl-soap12: Demonstrates how to develop, configure, and run a simple web services security sample

Directory path: install-dir/samples/java/webapps/webservices/security

Building the Samples

An ANT-based build system is used to build the individual sample application. The build.xml file has target to compile the sources, to clean, and to build the war file. It also has targets to deploy and undeploy the application, using the corresponding CLI commands provided by the Web Server. Register application resources in the deployment target.

Documentation for the Samples

Documentation is installed along with the samples during the installation. The index.html in the document root of the default Web Server instance contains links to the samples documentation. In addition, you can access the documentation HTML files directly in the samples directory.