KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > Lifecycle02


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

16
17 package org.apache.tester;
18
19
20 import java.io.*;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26
27 /**
28  * Positive test for servlet lifecycle management. This servlet is
29  * <strong>not</strong> declared to be load-on-startup, and the first request
30  * made to it should be a "POST".
31  *
32  * @author Craig R. McClanahan
33  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
34  */

35
36 public class Lifecycle02 extends HttpServlet {
37
38     private boolean doubled = false;
39
40     private boolean initialized = false;
41
42     public void init() throws ServletException {
43         if (initialized)
44             doubled = true;
45         else
46             initialized = true;
47     }
48
49     public void destroy() {
50         initialized = false;
51     }
52
53     public void doGet(HttpServletRequest request, HttpServletResponse response)
54         throws IOException, ServletException {
55
56         response.setContentType("text/plain");
57         PrintWriter writer = response.getWriter();
58         if (doubled) {
59             writer.println("Lifecycle02 FAILED - GET and double initialization");
60         } else if (initialized) {
61             writer.println("Lifecycle02 FAILED - GET called");
62         } else {
63             writer.println("Lifecycle02 FAILED - GET and not initialized");
64         }
65
66         while (true) {
67             String JavaDoc message = StaticLogger.read();
68             if (message == null)
69                 break;
70             writer.println(message);
71         }
72         StaticLogger.reset();
73
74     }
75
76     public void doPost(HttpServletRequest request, HttpServletResponse response)
77         throws IOException, ServletException {
78
79         response.setContentType("text/plain");
80         PrintWriter writer = response.getWriter();
81         if (doubled) {
82             writer.println("Lifecycle02 FAILED - Double initialization");
83         } else if (initialized) {
84             writer.println("Lifecycle02 PASSED");
85         } else {
86             writer.println("Lifecycle02 FAILED - POST but not initialized");
87         }
88
89         while (true) {
90             String JavaDoc message = StaticLogger.read();
91             if (message == null)
92                 break;
93             writer.println(message);
94         }
95         StaticLogger.reset();
96
97     }
98
99
100 }
101
Popular Tags