KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > FilterServlet


1 /*
2  * @(#)FilterServlet.java 1.3 02/12/20
3  *
4  * Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  *
38  */

39
40 package demo;
41
42 import java.io.*;
43 import javax.servlet.*;
44 import javax.servlet.http.*;
45
46 /**
47  * This servlet is used to determine whether the user is logged in before
48  * forwarding the request to the selected URL.
49  */

50 public class FilterServlet extends HttpServlet {
51
52     /**
53      * This method handles the "POST" submission from two forms: the
54      * login form and the message compose form.
55      */

56     public void doPost(HttpServletRequest request,
57                        HttpServletResponse response)
58                        throws IOException, ServletException {
59
60         String JavaDoc servletPath = request.getServletPath();
61         servletPath = servletPath.concat(".jsp");
62         
63         getServletConfig().getServletContext().
64             getRequestDispatcher("/" + servletPath).forward(request, response);
65     }
66
67     /**
68      * This method handles the GET requests from the client.
69      */

70     public void doGet(HttpServletRequest request,
71                       HttpServletResponse response)
72                       throws IOException, ServletException {
73       
74         // check to be sure we're still logged in
75
// before forwarding the request.
76
HttpSession session = request.getSession();
77         MailUserBean mailuser = (MailUserBean)session.getAttribute("mailuser");
78         String JavaDoc servletPath = request.getServletPath();
79         servletPath = servletPath.concat(".jsp");
80         
81         if (mailuser.isLoggedIn())
82             getServletConfig().getServletContext().
83                 getRequestDispatcher("/" + servletPath).
84                 forward(request, response);
85         else
86             getServletConfig().getServletContext().
87                 getRequestDispatcher("/index.html").
88                 forward(request, response);
89     }
90 }
91
92
Popular Tags