KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > docoriented > client > FrontController


1 /* Copyright 2005 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
2
3  http://developer.sun.com/berkeley_license.html
4
5  $Id: FrontController.java,v 1.15 2005/05/31 23:29:56 sean_brydon Exp $ */

6
7
8
9 package com.sun.j2ee.blueprints.docoriented.client;
10
11
12
13 import java.io.*;
14
15 import java.util.*;
16
17 import javax.servlet.*;
18
19 import javax.servlet.http.*;
20
21
22
23 /**
24
25  * Centralized controller for all the
26
27  * incoming requests
28
29  */

30
31 public class FrontController extends HttpServlet {
32
33     
34
35     private Map nameSpace;
36
37     private RequestHandler handler;
38
39
40
41     public void init() {
42
43         initPathMapping();
44
45         handler = new RequestHandler();
46
47     }
48
49
50
51     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws
52
53         IOException, ServletException {
54
55         doGet(req, resp);
56
57     }
58
59
60
61     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
62
63         IOException, ServletException{
64
65         process(req, resp);
66
67     }
68
69
70
71     protected void process(HttpServletRequest req, HttpServletResponse resp) throws
72
73                            IOException, ServletException {
74
75         resp.setContentType("text/html");
76
77         String JavaDoc responseURL = null;
78
79         String JavaDoc fullURL = req.getRequestURI();
80
81
82
83         // get the screen name
84

85         String JavaDoc selectedURL = null;
86
87         int lastPathSeparator = fullURL.lastIndexOf("/") + 1;
88
89         if (lastPathSeparator != -1) {
90
91             selectedURL = fullURL.substring(lastPathSeparator, fullURL.length());
92
93         }
94
95         responseURL = getResponseURL(selectedURL);
96
97         
98
99         if (selectedURL.equals("invokeservice.do")) {
100
101             try {
102
103                 handler.handle(req,resp);
104
105             } catch (RequestHandlerException re) {
106
107                 req.setAttribute("error_message", re.getMessage());
108
109                 responseURL = getResponseURL("error.do");
110
111             }
112
113         }
114
115         getServletConfig().getServletContext()
116
117                 .getRequestDispatcher(responseURL).forward(req, resp);
118
119     }
120
121
122
123     protected String JavaDoc getResponseURL(String JavaDoc url) {
124
125         return (String JavaDoc) nameSpace.get(url);
126
127     }
128
129                                                           
130
131     protected void initPathMapping() {
132
133         nameSpace = new HashMap();
134
135         nameSpace.put("invokeservice.do", "/result.jsp");
136
137         nameSpace.put("index.do", "/index.jsp");
138
139         nameSpace.put("error.do", "/error.jsp");
140
141     }
142
143 }
144
145
Popular Tags