Source code: com/sample/jsp/controller/servlet/TagExamplesServlet.java
1 /*
2 * TagExamplesServlet.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.sample.jsp.controller.servlet;
11
12 import java.io.*;
13
14 import java.util.Enumeration;
15
16 import javax.servlet.*;
17 import javax.servlet.http.*;
18
19 import com.aendvari.common.model.*;
20 import com.aendvari.common.model.xalan.*;
21
22 import com.aendvari.common.util.ResourceLoader;
23
24 import com.aendvari.satyr.servlet.ServletModelUtil;
25
26 import com.aendvari.tethys.tag.model.ModelTagData;
27 import com.aendvari.tethys.tag.inset.InsetTagData;
28 import com.aendvari.tethys.tag.logic.LogicTagData;
29 import com.aendvari.tethys.tag.message.MessageTagData;
30 import com.aendvari.tethys.tag.notices.NoticesTagData;
31
32 /**
33 * Servlet to run the example JSP pages.
34 *
35 * @author Scott Milne
36 *
37 */
38
39 public class TagExamplesServlet extends HttpServlet
40 {
41 /** Constants names. */
42 public interface Constants
43 {
44 // used by view classes for setting the name of the JSP resource for the servlet to load
45 final static String IncludeResource = "IncludeResource";
46
47 // used by the message broker
48 final static String Broker = "satyr/broker";
49
50 // for holding the ComponentDirectory instance
51 final static String ComponentDirectory = "satyr/directory";
52 }
53
54 /**
55 * Process an HTTP "GET" request.
56 *
57 * @param request The servlet request we are processing
58 * @param response The servlet response we are creating
59 *
60 * @exception IOException if an input/output error occurs
61 * @exception ServletException if a servlet exception occurs
62 *
63 */
64
65 public void doGet(HttpServletRequest request, HttpServletResponse response)
66 throws ServletException, IOException
67 {
68 ModelTree modelTree = null;
69
70 // retrieve model tree from session, if any
71 try
72 {
73 modelTree = ServletModelUtil.getModelTree(request.getSession());
74 }
75 catch (ServletModelUtil.NoModelException exception)
76 {
77 // there is no model tree currently in the users' session
78
79 // create an empty model tree, it will be loaded during login
80 modelTree = new XalanModelTree();
81
82 // set the model tree
83 ServletModelUtil.setModelTree(request.getSession(), modelTree);
84
85 // and load the xml template into the model
86 try
87 {
88 InputStream in = ResourceLoader.getResourceAsStream(
89 com.sample.jsp.data.XmlTestDataDescriptor.class);
90
91 modelTree.loadFromStream(in);
92 }
93 catch (ModelParserException e)
94 {
95 System.out.println(e);
96 throw new ServletException(e);
97 }
98 }
99
100 // provide the model
101 ModelTagData tagData = new ModelTagData(modelTree.getRootNode());
102 ModelTagData.setData(request, tagData);
103
104 // pass onto the given page
105 request.getRequestDispatcher(request.getPathInfo()).include(request, response);
106 }
107
108 /**
109 * Process an HTTP "POST" request.
110 *
111 * @param request The servlet request we are processing
112 * @param response The servlet response we are creating
113 *
114 * @exception IOException if an input/output error occurs
115 * @exception ServletException if a servlet exception occurs
116 *
117 */
118
119 public void doPost(HttpServletRequest request, HttpServletResponse response)
120 throws ServletException, IOException
121 {
122 ModelTree modelTree = null;
123
124 // check to see if there is still a valid session
125 // this is done by seeing if there is a valid modelTree available
126 try
127 {
128 modelTree = ServletModelUtil.getModelTree(request.getSession());
129 }
130 catch (ServletModelUtil.NoModelException exception)
131 {
132 doGet(request, response);
133 }
134
135 // otherwise, just print out the parameters and their values
136 response.setContentType("text/plain");
137
138 // get the parameters from the reaqest
139 Enumeration parameterNames = request.getParameterNames();
140
141 // get the print writer to show these back to the user
142 PrintWriter writer = response.getWriter();
143
144 // for each field sent from the form submission
145 while (parameterNames.hasMoreElements())
146 {
147 String name = (String)parameterNames.nextElement();
148 String parameterValues[] = request.getParameterValues(name);
149
150 writer.println(name);
151 for(int i=0; i<parameterValues.length; i++)
152 {
153 writer.println(" "+parameterValues[i]);
154 }
155
156 writer.println("");
157 }
158 }
159
160 /**
161 * Initialize the servlet.
162 *
163 * @exception ServletException if a servlet exception occurs
164 *
165 */
166
167 public void init()
168 throws ServletException
169 {
170 System.out.println("TagExamplesServlet initialized");
171 }
172 }
173