001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.examples.servlet;
028:
029: import java.io.IOException;
030: import java.io.PrintWriter;
031: import javax.servlet.http.HttpServlet;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034: import org.cougaar.core.servlet.ComponentServlet;
035:
036: /**
037: * This servlet illustrates an HTML form and URL-parameters passing.
038: * <p>
039: * To load this servlet, add the following to any agent's XML
040: * configuration:<pre>
041: * <component
042: * class="org.cougaar.core.examples.servlet.ColorServlet">
043: * <argument>/color</argument>
044: * </component>
045: * </pre>
046: */
047: public class ColorServlet extends ComponentServlet {
048:
049: public void doGet(HttpServletRequest request,
050: HttpServletResponse response) throws IOException {
051: // use a "worker" class, since multiple requests
052: // could happen at the same time (which would mangle
053: // our variables).
054: Worker w = new Worker(request, response);
055: w.execute();
056: }
057:
058: private static class Worker {
059: // from the "doGet(..)":
060: private HttpServletRequest request;
061: private HttpServletResponse response;
062:
063: // from URL-params:
064: // (this could be named "firstColor", but here
065: // we'll name it "c1" to show that these are
066: // separate concepts).
067: private static final String FIRST_COLOR_PARAM = "c1";
068: private String firstColor;
069:
070: // ditto for the "secondColor":
071: private static final String SECOND_COLOR_PARAM = "c2";
072: private String secondColor;
073:
074: // built-in colors for "secondColor":
075: private static final String[] COLORS = new String[] { "red",
076: "orange", "yellow", "green", "blue", "indigo",
077: "violet", };
078:
079: public Worker(HttpServletRequest request,
080: HttpServletResponse response) {
081: this .request = request;
082: this .response = response;
083: }
084:
085: public void execute() throws IOException {
086: parseParams();
087: writeResponse();
088: }
089:
090: private void parseParams() throws IOException {
091: firstColor = getParameter(FIRST_COLOR_PARAM, "black");
092: secondColor = getParameter(SECOND_COLOR_PARAM, COLORS[0]);
093: }
094:
095: private String getParameter(String name, String defaultValue) {
096: String value = request.getParameter(name);
097: if (value != null) {
098: value = value.trim();
099: if (value.length() == 0) {
100: value = null;
101: }
102: }
103: if (value == null) {
104: value = defaultValue;
105: }
106: return value;
107: }
108:
109: private void writeResponse() throws IOException {
110: response.setContentType("text/html");
111: PrintWriter out = response.getWriter();
112: // begin page:
113: out.print("<html><body>\n"
114: + "<h2>Example color-change page</h2><br>\n");
115: // begin form:
116: // (always use "GET", not "POST"!)
117: out.print("<form method=\"GET\" action=\"");
118: // form sends back to this servlet:
119: out.print(request.getRequestURI());
120: // show first color, let the user change it:
121: out.print("\">\n" + "First color is "
122: + "<font size=+2 color=\"" + firstColor + "\">"
123: + firstColor + "</font> (<tt>" + firstColor
124: + "</tt>), change to: " + "<input name=\""
125: + FIRST_COLOR_PARAM
126: + "\" type=\"text\" size=\"20\" value=\""
127: + firstColor + "\"><br>\n");
128: // just for kicks, lets use a drop-down list here:
129: out.print("Second color is " + "<font size=+2 color=\""
130: + secondColor + "\">" + secondColor
131: + "</font> (<tt>" + secondColor
132: + "</tt>), change to: " + "<select name=\""
133: + SECOND_COLOR_PARAM + "\">\n");
134: for (int i = 0; i < COLORS.length; i++) {
135: String ci = COLORS[i];
136: out.print("<option");
137: if (ci.equals(secondColor)) {
138: out.print(" selected");
139: }
140: out.print(">" + ci + "</option>\n");
141: }
142: out.print("</select><br>\n");
143: // add form button:
144: out
145: .print("<input type=\"submit\" value=\"change colors!\"><br>\n");
146: // end form:
147: out.print("</form>\n");
148: // end page:
149: out.print("</body></html>");
150: }
151: }
152: }
|