Chapter 7. Execution Control

Introduction

This chapter presents problems related to controlling execution of your web application. Some of the solutions use listener features provided by the Servlet container. These classes and interfaces allow your application to receive notification of servlet container events. Servlet listeners can be used on any Java web application and container that supports the Servlet 2.3 API.

Managing workflow in a web application can be a challenge due to the stateless nature of HTTP. This chapter covers some approaches for handling navigation within a Struts application. Finally, this chapter will look at some specific problems and solutions you’ll encounter when working with files. You’ll find solutions for enabling and processing file uploads as well as displaying content—in varying formats—from files on the server.

7.1. Performing Tasks at Application Startup

Problem

You want to be notified when your web application is initialized so you can preload application-scope data or execute other startup functions.

Solution

Create a class that implements the ServletContextListener interface. The class shown in Example 7-1 stores the current date and time in the servlet context when the application is started.

Example 7-1. Servlet context data loader
package com.oreilly.strutsckbk.ch07; import java.util.Date; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class ContextLoader implements ServletContextListener { public ...

Get Jakarta Struts Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.