KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > SampleServlet


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.sample.servlet;
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24
25 import java.util.Hashtable JavaDoc;
26
27 import javax.servlet.RequestDispatcher JavaDoc;
28 import javax.servlet.ServletConfig JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.http.Cookie JavaDoc;
31 import javax.servlet.http.HttpServlet JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import javax.servlet.http.HttpSession JavaDoc;
35
36 /**
37  * Sample servlet that implement some very simple business logic. The goal is
38  * to provide functional tests for Cactus, so we focus on providing as many
39  * different test cases as possible rather than implementing a meaningful
40  * business logic.
41  *
42  * @version $Id: SampleServlet.java,v 1.3 2004/02/29 16:36:45 vmassol Exp $
43  */

44 public class SampleServlet extends HttpServlet JavaDoc
45 {
46     /**
47      * Entry point for the servlet when a GET request is received. This will
48      * be used to verify that we can test for the servlet output stream in
49      * Cactus test cases.
50      *
51      * @param theRequest the HTTP request
52      * @param theResponse the HTTP response
53      *
54      * @exception IOException on failure
55      */

56     public void doGet(HttpServletRequest JavaDoc theRequest,
57         HttpServletResponse JavaDoc theResponse) throws IOException JavaDoc
58     {
59         PrintWriter JavaDoc pw = theResponse.getWriter();
60
61         theResponse.setContentType("text/html");
62
63         pw.print("<html><head/><body>");
64         pw.print("A GET request");
65         pw.print("</body></html>");
66     }
67
68     /**
69      * Return the method used to send data from the client (POST or GET). This
70      * will be used to verify that we can simulate POST or GET methods from
71      * Cactus. This simulates a method which would test the method to
72      * implement it's business logic.
73      *
74      * @param theRequest the HTTP request
75      * @return the method used to post data
76      */

77     public String JavaDoc checkMethod(HttpServletRequest JavaDoc theRequest)
78     {
79         return theRequest.getMethod();
80     }
81
82     /**
83      * Set some variable in the HTTP session. It verifies that a session object
84      * has automatically been created by Cactus prior to calling this method.
85      *
86      * @param theRequest the HTTP request
87      */

88     public void setSessionVariable(HttpServletRequest JavaDoc theRequest)
89     {
90         HttpSession JavaDoc session = theRequest.getSession(false);
91
92         session.setAttribute("name_setSessionVariable",
93             "value_setSessionVariable");
94     }
95
96     /**
97      * Set some attribute in the request.
98      *
99      * @param theRequest the HTTP request
100      */

101     public void setRequestAttribute(HttpServletRequest JavaDoc theRequest)
102     {
103         theRequest.setAttribute("name_setRequestAttribute",
104             "value_setRequestAttribute");
105     }
106
107     /**
108      * Get some parameters from the HTTP request.
109      *
110      * @param theRequest the HTTP request
111      * @return a hashtable containing some parameters
112      */

113     public Hashtable JavaDoc getRequestParameters(HttpServletRequest JavaDoc theRequest)
114     {
115         Hashtable JavaDoc params = new Hashtable JavaDoc();
116
117         params.put("param1", theRequest.getParameter("param1"));
118         params.put("param2", theRequest.getParameter("param2"));
119
120         return params;
121     }
122
123     /**
124      * Get a header from the request.
125      *
126      * @return a test request header
127      * @param theRequest the HTTP request
128      */

129     public String JavaDoc getRequestHeader(HttpServletRequest JavaDoc theRequest)
130     {
131         return theRequest.getHeader("testheader");
132     }
133
134     /**
135      * @return the cookies sent in the HTTP request
136      *
137      * @param theRequest the HTTP request
138      */

139     public Hashtable JavaDoc getRequestCookies(HttpServletRequest JavaDoc theRequest)
140     {
141         Hashtable JavaDoc allCookies = new Hashtable JavaDoc();
142
143         Cookie JavaDoc[] cookies = theRequest.getCookies();
144
145         if (cookies != null)
146         {
147             for (int i = 0; i < cookies.length; i++)
148             {
149                 Cookie JavaDoc cookie = cookies[i];
150
151                 allCookies.put(cookie.getName(), cookie.getValue());
152             }
153         }
154
155         return allCookies;
156     }
157
158     /**
159      * Set a header in the HTTP response. This is to verify that Cactus tests
160      * can assert the returned headers.
161      *
162      * @param theResponse the HTTP response
163      */

164     public void setResponseHeader(HttpServletResponse JavaDoc theResponse)
165     {
166         theResponse.setHeader("responseheader", "this is a response header");
167     }
168
169     /**
170      * Set a cookie for sending back to the client. This is to verify that
171      * it is possible with Cactus to assert the cookies returned to the client
172      *
173      * @param theResponse the HTTP response
174      */

175     public void setResponseCookie(HttpServletResponse JavaDoc theResponse)
176     {
177         Cookie JavaDoc cookie = new Cookie JavaDoc("responsecookie",
178             "this is a response cookie");
179
180         cookie.setDomain("jakarta.apache.org");
181         theResponse.addCookie(cookie);
182     }
183
184     /**
185      * Use a <code>RequestDispatcher</code> to forward to a JSP page. This is
186      * to verify that Cactus supports asserting the result, even in the case
187      * of forwarding to another page.
188      *
189      * @param theRequest the HTTP request
190      * @param theResponse the HTTP response
191      * @param theConfig the servlet config object
192      *
193      * @exception IOException on failure
194      * @exception ServletException on failure
195      */

196     public void doForward(HttpServletRequest JavaDoc theRequest,
197         HttpServletResponse JavaDoc theResponse, ServletConfig JavaDoc theConfig)
198         throws IOException JavaDoc, ServletException JavaDoc
199     {
200         RequestDispatcher JavaDoc rd =
201             theConfig.getServletContext().getRequestDispatcher(
202             "/test/test.jsp");
203
204         rd.forward(theRequest, theResponse);
205     }
206
207     /**
208      * Use a <code>RequestDispatcher</code> to include a JSP page. This is
209      * to verify that Cactus supports asserting the result, even in the case
210      * of including another page.
211      *
212      * @param theRequest the HTTP request
213      * @param theResponse the HTTP response
214      * @param theConfig the servlet config object
215      *
216      * @exception IOException on failure
217      * @exception ServletException on failure
218      */

219     public void doInclude(HttpServletRequest JavaDoc theRequest,
220         HttpServletResponse JavaDoc theResponse, ServletConfig JavaDoc theConfig)
221         throws IOException JavaDoc, ServletException JavaDoc
222     {
223         RequestDispatcher JavaDoc rd =
224             theConfig.getServletContext().getRequestDispatcher(
225             "/test/test.jsp");
226
227         rd.include(theRequest, theResponse);
228     }
229 }
230
Popular Tags