Web Development with JavaServer Pages

Скачать в pdf «Web Development with JavaServer Pages»

You are taken to the main menu via the main-menu command. Visiting the main menu is also the default activity if no command identifier is specified. In either case, no action is required, and the command is handled by a very simple implementation of the Command interface called NullCommand.

The NullCommand class

The simplest of our commands, as you might expect, is the NullCommand class (
listing 11.8). It simply returns its next URL value, performing no operation. This class is used for commands that are simply requests to visit a particular page, such as visiting the main menu and collecting the information necessary to add an FAQ to the database.

An example JSP project

Listing 11.8 NullCommand

package com.taglib.wdj sp.faqtool;

import javax.servlet.*;

import javax.servlet.http.*;

public class NullCommand implement s Command { private String next;

public NullCommand(String next) { this.next = next;


public String execute(HttpServletRequest req) throws CommandException { return next;



The AbortCommand class

We also created an AbortCommand class to handle the case where the user wants to abort the current operation and return to the main menu from any page. AbortCommand differs from NullCommand in only one way: it adds a message to the request in the form of a request attribute—creating a simple page-to-page communication system. This message is retrieved by the main menu JSP page, and used to update the status area of the main menu interface (figure 11.3.) This is a way to give feedback to the user about the status of the last operation. We’ll use this technique in several other commands as well. The AbortComand code is shown in listing 11.9.

Скачать в pdf «Web Development with JavaServer Pages»