01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.geronimo.test;
17:
18: import java.io.IOException;
19: import java.io.PrintWriter;
20:
21: import javax.naming.Context;
22: import javax.naming.InitialContext;
23: import javax.servlet.ServletException;
24: import javax.servlet.ServletConfig;
25: import javax.servlet.http.HttpServlet;
26: import javax.servlet.http.HttpServletRequest;
27: import javax.servlet.http.HttpServletResponse;
28: import javax.servlet.http.HttpSession;
29:
30: public class TestServlet extends HttpServlet {
31:
32: private static Integer expectedValue = new Integer(20);
33:
34: protected void doGet(HttpServletRequest request,
35: HttpServletResponse response) throws ServletException,
36: IOException {
37: String mode = request.getParameter("mode");
38:
39: if ("forward".equals(mode)) {
40: getServletConfig().getServletContext().getContext(
41: "/dispatch1").getRequestDispatcher("/TestServlet")
42: .forward(request, response);
43: } else {
44: PrintWriter out = response.getWriter();
45: Integer value = lookup("java:comp/env/value", request);
46:
47: out.println("TestServlet2: " + value);
48:
49: // create new session for testing
50: HttpSession session = request.getSession(true);
51: session.setAttribute("sessAttr1", "value21");
52: }
53: }
54:
55: protected void doPost(HttpServletRequest request,
56: HttpServletResponse response) throws ServletException,
57: IOException {
58: }
59:
60: public void init(ServletConfig config) throws ServletException {
61: super .init(config);
62: testLookup("servletInit");
63: }
64:
65: public void destroy() {
66: testLookup("servletDestroy");
67: }
68:
69: private Integer lookup(String name, HttpServletRequest request) {
70: try {
71: Context ctx = new InitialContext();
72: Integer value = (Integer) ctx.lookup(name);
73: return value;
74: } catch (Exception e) {
75: e.printStackTrace();
76: return null;
77: }
78: }
79:
80: public static void testLookup(String name) {
81: System.out.println(name);
82:
83: Integer value;
84: try {
85: Context ctx = new InitialContext();
86: value = (Integer) ctx.lookup("java:comp/env/value");
87: } catch (Exception e) {
88: e.printStackTrace();
89: throw new RuntimeException(name, e);
90: }
91:
92: if (!expectedValue.equals(value)) {
93: throw new RuntimeException("Values do not match: "
94: + expectedValue + " " + value);
95: }
96: }
97:
98: }
|