KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kelp > webapp > presentation > FormServlet


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22 package kelp.webapp.presentation;
23
24 // Xerces imports
25
import org.w3c.dom.html.HTMLInputElement;
26
27 // Servlet imports
28
import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.ServletOutputStream JavaDoc;
30 import javax.servlet.http.HttpServlet JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.http.HttpSession JavaDoc;
34
35 // Standard imports
36
import java.io.IOException JavaDoc;
37 import java.io.File JavaDoc;
38 import java.io.FileInputStream JavaDoc;
39 import java.io.FileOutputStream JavaDoc;
40 import java.util.Properties JavaDoc;
41
42 /**
43  * <p>
44  * This presentation object dynamically creates an HTML page
45  * showing the greeting from the application configuration file.
46  * </p>
47  */

48 public class FormServlet extends HttpServlet JavaDoc {
49     public void doPost(HttpServletRequest JavaDoc request,
50                        HttpServletResponse JavaDoc response) throws ServletException JavaDoc,
51                        IOException JavaDoc {
52         doGet(request, response);
53     }
54
55     public void doGet(HttpServletRequest JavaDoc request,
56                       HttpServletResponse JavaDoc response) throws ServletException JavaDoc,
57                       IOException JavaDoc {
58         FormHTML page = null;
59         ServletOutputStream JavaDoc out;
60         Properties JavaDoc data = null;
61         byte[] buffer;
62
63         data = initData(request, data);
64         data = saveParameters(request, data);
65         page = createPage(data);
66         buffer = page.toDocument().getBytes();
67         response.setContentType("text/html");
68         response.setContentLength(buffer.length);
69         out = response.getOutputStream();
70         out.write(buffer);
71         out.flush();
72         response.flushBuffer();
73     }
74
75     private Properties JavaDoc saveParameters(HttpServletRequest JavaDoc request,
76                                       Properties JavaDoc data) {
77         String JavaDoc paramFirst = null;
78         String JavaDoc paramLast = null;
79
80         try {
81             paramFirst = request.getParameter("first");
82             paramLast = request.getParameter("last");
83         } catch (Exception JavaDoc e) {
84             paramFirst = null;
85             paramLast = null;
86         }
87         if (paramFirst != null && paramLast != null) {
88             data.put("first", paramFirst);
89             data.put("last", paramLast);
90             writePropertyFile(data);
91         }
92         return data;
93     }
94
95     public FormHTML createPage(Properties JavaDoc data) {
96         String JavaDoc first = null;
97         String JavaDoc last = null;
98         HTMLInputElement inputFirst = null;
99         HTMLInputElement inputLast = null;
100         FormHTML page = null;
101
102         page = new FormHTML();
103         first = (String JavaDoc) data.get("first");
104         last = (String JavaDoc) data.get("last");
105         inputFirst = page.getElementFirst();
106         inputLast = page.getElementLast();
107         inputFirst.setValue(first);
108         inputLast.setValue(last);
109         return page;
110     }
111
112     private void writePropertyFile(Properties JavaDoc data) {
113         FileOutputStream JavaDoc out = null;
114         String JavaDoc dataPath = null;
115
116         dataPath = getDataPath();
117         try {
118             out = new FileOutputStream JavaDoc(dataPath, false);
119             data.store(out, "Data for Form page");
120             out.close();
121         } catch (java.io.IOException JavaDoc e) {
122             e.printStackTrace();
123         }
124     }
125
126     private Properties JavaDoc readPropertyFile(Properties JavaDoc data) {
127         FileInputStream JavaDoc is = null;
128         String JavaDoc dataPath = null;
129
130         dataPath = getDataPath();
131         try {
132             is = new FileInputStream JavaDoc(dataPath);
133             data = new Properties JavaDoc();
134             data.load(is);
135             is.close();
136         } catch (Exception JavaDoc e) {
137
138             // create deault
139
e.printStackTrace();
140             data = new Properties JavaDoc();
141             data.put("first", "Luke");
142             data.put("last", "Skywalker");
143         }
144         return data;
145     }
146
147     private Properties JavaDoc initData(HttpServletRequest JavaDoc request, Properties JavaDoc data) {
148         HttpSession JavaDoc session = null;
149
150         session = request.getSession(true);
151
152         if (session != null) {
153           data = (Properties JavaDoc) session.getAttribute("data");
154         }
155         if (data == null) {
156             data = readPropertyFile(data);
157         }
158         return data;
159     }
160
161     private String JavaDoc getDataPath() {
162         StringBuffer JavaDoc pathBuf = new StringBuffer JavaDoc();
163         String JavaDoc testPath = getInitParameter("testDataPath");
164
165         if (testPath == null) {
166             System.out.println("init-parm not found: testDataPath");
167         } else {
168             pathBuf.append(testPath);
169
170             // Remove quotes
171
int lastAt = -1;
172
173             lastAt = pathBuf.length() - 1;
174             if (pathBuf.charAt(lastAt) == '"') {
175                 pathBuf.deleteCharAt(lastAt);
176             }
177             if (pathBuf.charAt(0) == '"') {
178                 pathBuf.deleteCharAt(0);
179             }
180             pathBuf.append('/');
181             pathBuf.append("form.properties");
182         }
183         return pathBuf.toString();
184     }
185
186 }
187
Popular Tags