KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > web > servlets > APIServlet


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.web.servlets;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import javax.servlet.ServletConfig JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.http.HttpServlet JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import javax.servlet.http.HttpSession JavaDoc;
32
33 import org.jboss.test.web.util.Util;
34
35 /** A servlet that tests use of various servlet API calls that can be affected
36  by the web container integration layer.
37  
38  @author Scott.Stark@jboss.org
39  @version $Revision: 37406 $
40  */

41 public class APIServlet extends HttpServlet JavaDoc
42 {
43    protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
44       throws ServletException JavaDoc, IOException JavaDoc
45    {
46       response.setContentType("text/html");
47       PrintWriter JavaDoc out = response.getWriter();
48       out.println("<html>");
49       out.println("<head><title>APIServlet</title></head><body><pre>");
50       String JavaDoc op = request.getParameter("op");
51       if( op.equals("testGetRealPath") )
52       {
53          String JavaDoc realPath = testGetRealPath();
54          out.println("testGetRealPath ok, realPath="+realPath+"\n");
55       }
56       else if( op.equals("testSessionListener") )
57       {
58          testSessionListener(request);
59       }
60       else
61       {
62          throw new ServletException JavaDoc("Unknown operation called, op="+op);
63       }
64       out.println("</pre></body></html>");
65       out.close();
66    }
67
68    protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
69       throws ServletException JavaDoc, IOException JavaDoc
70    {
71       processRequest(request, response);
72    }
73
74    protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
75       throws ServletException JavaDoc, IOException JavaDoc
76    {
77       processRequest(request, response);
78    }
79
80    private String JavaDoc testGetRealPath()
81       throws ServletException JavaDoc
82    {
83       String JavaDoc realPath = getServletContext().getRealPath("/");
84       if( realPath == null )
85          throw new ServletException JavaDoc("getServletContext().getRealPath(/) returned null");
86       return realPath;
87    }
88
89    private void testSessionListener(HttpServletRequest JavaDoc request)
90       throws ServletException JavaDoc
91    {
92       // Create/get the session
93
HttpSession JavaDoc session = request.getSession(true);
94       String JavaDoc sessionID = session.getId();
95       boolean created = TestSessionListener.wasCreated(sessionID);
96       if( created == false )
97          throw new ServletException JavaDoc("No session create event seen");
98       // Invalidate the session to test the destroy event
99
session.invalidate();
100       boolean destroyed = TestSessionListener.wasDestroyed(sessionID);
101       if( destroyed == false )
102          throw new ServletException JavaDoc("No session destroy event seen");
103    }
104 }
105
Popular Tags