KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ajaxtags > servlets > BaseAjaxServlet


1 /**
2  * Copyright 2005 Darren L. Spurgeon
3  * Copyright 2007 Jens Kapitza
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.ajaxtags.servlets;
18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServlet JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27
28 /**
29  * An abstract class from which each example servlet extends. This class wraps
30  * the XML creation (delegated to the child servlet class) and submission back
31  * through the HTTP response.
32  *
33  * @author Darren L. Spurgeon
34  * @author Jens Kapitza
35  * @version $Revision: 1.8 $ $Date: 2007/07/22 16:29:16 $ $Author: jenskapitza $
36  */

37 public abstract class BaseAjaxServlet extends HttpServlet JavaDoc implements BaseAjaxXmlAction {
38
39     /**
40      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
41      * javax.servlet.http.HttpServletResponse)
42      */

43     @Override JavaDoc
44     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
45             throws ServletException JavaDoc, IOException JavaDoc {
46
47         try {
48             String JavaDoc xml = null;
49             xml = AjaxActionHelper.invoke(this, request, response);
50             PrintWriter JavaDoc pw = response.getWriter();
51             pw.write(xml);
52             pw.close();
53
54         } catch (Exception JavaDoc ex) {
55             // Send back a 500 error code.
56
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
57                     "Can not create response");
58             return;
59         }
60     }
61
62     /**
63      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
64      * javax.servlet.http.HttpServletResponse)
65      */

66     @Override JavaDoc
67     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
68             throws ServletException JavaDoc, IOException JavaDoc {
69         doGet(request, response);
70     }
71
72     /**
73      * Each child class should override this method to generate the specific XML
74      * content necessary for each AJAX action.
75      *
76      * @param request
77      * the {@javax.servlet.http.HttpServletRequest} object
78      * @param response
79      * the {@javax.servlet.http.HttpServletResponse} object
80      * @return a {@java.lang.String} representation of the XML response/content
81      */

82     public abstract String JavaDoc getXmlContent(HttpServletRequest JavaDoc request,
83             HttpServletResponse JavaDoc response) throws Exception JavaDoc;
84 }
85
Popular Tags