1   //==============================================================================
2   // file :       CommandController.java
3   // project:     Front View Control
4   //
5   // last change: date:       $Date: 2003/09/10 09:13:57 $
6   //              by:         $Author: bitiboy $
7   //              revision:   $Revision: 1.1 $
8   //------------------------------------------------------------------------------
9   // copyright:   GNU GPL Software License (see class documentation)
10  //==============================================================================
11  package com.justhis.control;
12  
13  
14  /*
15   * $Id: CommandController.java,v 1.1 2003/09/10 09:13:57 bitiboy Exp $
16   *
17   * Copyright 2003 Acai Software All Rights Reserved.
18   *
19   * This file CommandController.java is part of the Front View Control.
20  
21   * The Front View Control is free software; you can redistribute it and/or modify
22   * it under the terms of the GNU General Public License as published by
23   * the Free Software Foundation; either version 2 of the License, or
24   * (at your option) any later version.
25  
26   * The Front View Control is distributed in the hope that it will be useful,
27   * but WITHOUT ANY WARRANTY; without even the implied warranty of
28   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29   * GNU General Public License for more details.
30  
31   * You should have received a copy of the GNU General Public License
32   * along with the Front View Control; if not, write to the Free Software
33   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
34  
35   * http://www.justhis.com http://ejb.cn
36   * CONTACT: email = superaxis@sohu.com webmaster@justhis.com
37   */
38  import com.justhis.util.exception.JavaClassRefectException;
39  import com.justhis.util.exception.LogicException;
40  import com.justhis.util.exception.UtilException;
41  
42  import java.io.IOException;
43  
44  import java.sql.SQLException;
45  
46  import javax.servlet.RequestDispatcher;
47  import javax.servlet.Servlet;
48  import javax.servlet.ServletConfig;
49  import javax.servlet.ServletException;
50  import javax.servlet.http.HttpServlet;
51  import javax.servlet.http.HttpServletRequest;
52  import javax.servlet.http.HttpServletResponse;
53  
54  
55  /***
56   * controller of MVC. control system action;
57   *
58   * @author <a href="http://blog.ejb.cn">acai</a>
59   * @version $Revision: 1.1 $
60   */
61  public class CommandController extends HttpServlet implements Servlet {
62      //~ Instance fields --------------------------------------------------------
63  
64      /*** Servlet config */
65      private ServletConfig config;
66  
67      //~ Methods ----------------------------------------------------------------
68  
69      /***
70       * Init the servlet
71       */
72      public final void init(ServletConfig config) throws ServletException {
73          this.config = config;
74          System.out.println("#####################################################");
75          System.out.println("#           East News System now init!--by acai     #");
76          System.out.println("#####################################################");
77      }
78  
79      /***
80       * TODO
81       *
82       * @return TODO
83       */
84      public final ServletConfig getServletConfig() {
85          return config;
86      }
87  
88      /***
89       * Process a request.
90       *
91       * @param request the request
92       * @param response the response
93       */
94      private void processRequest(ServletConfig config,
95                                  HttpServletRequest request,
96                                  HttpServletResponse response
97                                 ) throws ServletException, IOException {
98          try {
99              request.setCharacterEncoding("UTF-8");
100             response.setContentType("text/xml;charset=UTF-8");
101 
102             CommandDispatcher dispatcher = new CommandDispatcher(config,
103                                                                  request,
104                                                                  response
105                                                                 );
106             String next = dispatcher.executeCommand();
107 
108             dispatch(request, response, next);
109         } catch (ServletException e) {
110             dispatchErrorPage(e, request, response);
111         } catch (UtilException e) {
112             dispatchErrorPage(e, request, response);
113         } catch (JavaClassRefectException e) {
114             dispatchErrorPage(e, request, response);
115         } catch (LogicException e) {
116             dispatchErrorPage(e, request, response);
117         } catch (SQLException e) {
118             dispatchErrorPage(e, request, response);
119         } catch (Exception e) {
120             dispatchErrorPage(e, request, response);
121         }
122     }
123 
124     /***
125      * Send an error page
126      *
127      * @param request
128      * @param response
129      */
130     private void dispatchErrorPage(Exception e, HttpServletRequest request,
131                                    HttpServletResponse response
132                                   ) throws ServletException, IOException {
133         e.printStackTrace();
134         request.setAttribute("msg", e.getMessage());
135         request.setAttribute("e", e);
136         dispatch(request, response, "/error.jsp");
137     }
138 
139     /***
140      * @see HttpServlet
141      */
142     protected void doGet(HttpServletRequest request,
143                          HttpServletResponse response
144                         ) throws ServletException, IOException {
145         processRequest(config, request, response);
146     }
147 
148     /***
149      * @see HttpServlet
150      */
151     protected void doPost(HttpServletRequest request,
152                           HttpServletResponse response
153                          ) throws ServletException, IOException {
154         processRequest(config, request, response);
155     }
156 
157     /***
158      * Dispatch a request to a page.
159      *
160      * @param request the request
161      * @param response the response
162      */
163     private void dispatch(HttpServletRequest request,
164                           HttpServletResponse response, String page
165                          ) throws ServletException, IOException {
166         if (page == null) {
167             return;
168         }
169 
170         RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(page);
171         dispatcher.forward(request, response);
172     }
173 
174     /***
175      * @see javax.servlet.Servlet#destroy()
176      */
177     public void destroy() {
178         System.out.println("#####################################################");
179         System.out.println("#      East News Sytem now destory!--by acai        #");
180         System.out.println("#            Close All Resources.Thank You!         #");
181         System.out.println("#####################################################");
182 
183         /*????????????????????*/
184     }
185 }
186 
187 
188 /*
189  * $Log: CommandController.java,v $
190  * Revision 1.1  2003/09/10 09:13:57  bitiboy
191  * *** empty log message ***
192  *
193  *
194 */
This page was automatically generated by Maven