Web Development with JavaServer Pages

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

Listing 11.9 AbortCommand

package com.taglib.wdj sp.faqtool;

import javax.servlet.*; import javax.servlet.http.*;

public class AbortCommand implements Command { private String next;

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


public String execute(HttpServletRequest req) throws CommandException {

req.setAttribute(«faqtool.msg», «Operation Aborted»); return next;


Figure 11.3 A status message on the main menu

The main menu JSP page

The operation of this page is straightforward. The main menu page allows the user to add, update, or delete an FAQ from the database. That is the page’s only job. The source code for the main menu page, menu.jsp is shown in listing 11.10.

Listing 11.10 menu.jsp

<%@ page import=,,com.taglib.wdjsp.faqtool.*» errorPage=n/jsp/error.jspn %>



<title>Main Menu</title>

<script language=»JavaScript»> function setCmd(value) {

document.menu.cmd.value = value;




<body bgcolor=»white»>

form name=»menu» action=»/faqtool» method=»post»>

An example JSP project

<input type=»hidden» name=»cmd» value=»»>

<table bgcolor=»tan» border=»0″ align=»center» cellpadding=»10″>

<tr><th>FAQ Administration: Main Menu</th></tr>

<tr><td align=»center»>

<input type=»submit» value=»Create New FAQ» onClick=»setCmd(‘add’)»></td></tr>

<tr><td align=»center»>

<input type=»submit» value=»Update An Existing FAQ» onClick=»setCmd(‘update-menu’)»></td></tr>

<tr><td align=»center»>

<input type=»submit» value=»Delete An Existing FAQ» onClick=»setCmd(‘delete-menu’)»></td></tr>

<tr><td bgcolor=»white»><font size=»-1″>

<% if (request.getAttribute(nfaqtool.msgn) != null) { %>

<i><%= request.getAttribute(«faqtool.msg») %></i>

<% } %>






We’ve created a simple form, which, upon submittal, posts the form data back to the URL /faqtool, which we’ve mapped to the FaqAdminServlet in our JSP container. The command action will be specified through the request parameter cmd, which must be set by our form. There are a number of ways to include this request parameter into our form submission. We could have three separate forms on the page each with its own appropriate values assigned to the hidden element called cmd, and the three selection buttons would be the submit buttons for each form. We could also have named our submit buttons cmd, and set the value of each to the appropriate command identifiers. We could have even used anchor tags with URLs such as the following, which encode the cmd identifier into the URL as a parameter:

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