KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > clientstate > WordServlet


1 /* Copyright 2004 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
2  http://adventurebuilder.dev.java.net/LICENSE.txt
3  $Id: WordServlet.java,v 1.2 2005/04/20 21:01:54 gmurray71 Exp $ */

4
5 package com.sun.j2ee.blueprints.clientstate;
6
7
8 import java.io.*;
9 import java.util.*;
10
11 // Apache Commons- Tag-Lib Imports
12
import org.apache.commons.codec.base64.Base64;
13
14 // J2EE imports
15
import javax.servlet.*;
16 import javax.servlet.http.*;
17
18 import com.sun.j2ee.blueprints.clientstate.deserialize.*;
19
20 /**
21  * This Servlet will modify the contents of the SampleBean.
22  *
23 */

24 public class WordServlet extends HttpServlet {
25
26     private ServletContext context = null;
27     
28     public void init(ServletConfig config) throws ServletException {
29         this.context = config.getServletContext();
30     }
31     
32     public void doPost(HttpServletRequest request,
33                             HttpServletResponse response)
34                                throws IOException, ServletException {
35         // deserialize any state that might be in the page
36
ClientStateDeserializer.deserialize(request,response);
37         SampleBean bean = (SampleBean)request.getAttribute("sampleBean");
38         if (bean == null) {
39             System.out.println("WordServlet: Creating a new SampleBean");
40             bean = new SampleBean();
41             request.setAttribute("sampleBean", bean);
42         }
43         String JavaDoc newWord = request.getParameter("newWord");
44         if (!newWord.trim().equals("")) {
45                 bean.addWord(newWord);
46         }
47         // remove words that are checked
48
Map params = (Map)request.getParameterMap();
49         Iterator it = params.keySet().iterator();
50         while (it.hasNext()) {
51             String JavaDoc key = (String JavaDoc)it.next();
52             if (key.startsWith("word_")) {
53                 String JavaDoc[] values = (String JavaDoc[])params.get(key);
54                 String JavaDoc valueString = values[0];
55                 bean.removeWord(valueString);
56             }
57         }
58         context.getRequestDispatcher("/index.jsp").forward(request, response);
59     }
60 }
61
62
Popular Tags