KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > servlets > MVCServlet


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.servlets;
14
15 import java.io.IOException JavaDoc;
16
17 import javax.servlet.ServletException JavaDoc;
18 import javax.servlet.http.HttpServlet JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 import org.apache.log4j.Logger;
23
24
25 /**
26  * The servlet gets a MVCHandler with the method getHandler. The method getCommand of this returned handler is called to
27  * map the request parameters to a command. Then execute() is called which uses reflection to call a method. Each method
28  * returns a string defining the view. After that, renderHtml is called.
29  * <p>
30  * Make a subclass to provide you own handler(s).
31  * @author Philipp Bracher
32  * @version $Id: AdminInterfaceServlet.java 661 2005-05-03 14:10:45Z philipp $
33  */

34 public abstract class MVCServlet extends HttpServlet JavaDoc {
35
36     /**
37      * Stable serialVersionUID.
38      */

39     private static final long serialVersionUID = 222L;
40
41     private static Logger log = Logger.getLogger(MVCServlet.class);
42
43     /**
44      * @see HttpServlet#doPost(HttpServletRequest,HttpServletResponse)
45      */

46     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc,
47         IOException JavaDoc {
48         doGet(request, response);
49     }
50
51     /**
52      * @see HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
53      */

54     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
55
56         // http://issues.apache.org/bugzilla/show_bug.cgi?id=22666
57
//
58
// 1. The Coyote HTTP/1.1 connector has a useBodyEncodingForURI attribute which
59
// if set to true will use the request body encoding to decode the URI query
60
// parameters.
61
// - The default value is true for TC4 (breaks spec but gives consistent
62
// behaviour across TC4 versions)
63
// - The default value is false for TC5 (spec compliant but there may be
64
// migration issues for some apps)
65
// 2. The Coyote HTTP/1.1 connector has a URIEncoding attribute which defaults to
66
// ISO-8859-1.
67
// 3. The parameters class (o.a.t.u.http.Parameters) has a QueryStringEncoding
68
// field which defaults to the URIEncoding. It must be set before the parameters
69
// are parsed to have an effect.
70
//
71
// Things to note regarding the servlet API:
72
// 1. HttpServletRequest.setCharacterEncoding() normally only applies to the
73
// request body NOT the URI.
74
// 2. HttpServletRequest.getPathInfo() is decoded by the web container.
75
// 3. HttpServletRequest.getRequestURI() is not decoded by container.
76
//
77
// Other tips:
78
// 1. Use POST with forms to return parameters as the parameters are then part of
79
// the request body.
80

81         // this can throw an exception in jetty
82
try {
83             request.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
84
}
85         catch (java.lang.IllegalStateException JavaDoc e) {
86             log.error("can't set character encoding for the request", e); //$NON-NLS-1$
87
}
88
89         // why do i have to change it if request was setted? But i have to!
90
response.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
91

92         response.setContentType("text/html; charset=UTF-8"); //$NON-NLS-1$
93

94         MVCServletHandler handler = getHandler(request, response);
95
96         if (handler == null) {
97             log.error("no handler found"); //$NON-NLS-1$
98
return;
99         }
100
101         if (log.isDebugEnabled()) {
102             log.debug("handler: " + handler.getName()); //$NON-NLS-1$
103
}
104         String JavaDoc command = handler.getCommand();
105         if (log.isDebugEnabled()) {
106             log.debug("calling command: " + command); //$NON-NLS-1$
107
}
108         String JavaDoc view = handler.execute(command);
109         if (log.isDebugEnabled()) {
110             log.debug("calling view: " + view); //$NON-NLS-1$
111
}
112
113         handler.renderHtml(view);
114
115     }
116
117     /**
118      * @param request
119      * @return
120      */

121     protected abstract MVCServletHandler getHandler(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response);
122 }
Popular Tags