KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubbos > servlets > BrowseCategories


1 /**
2  * RUBBoS: Rice University Bulletin Board System.
3  * Copyright (C) 2001-2004 Rice University and French National Institute For
4  * Research In Computer Science And Control (INRIA).
5  * Contact: jmob@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Niraj Tolia.
23  */

24
25 package edu.rice.rubbos.servlets;
26
27 import java.io.IOException JavaDoc;
28 import java.net.URLEncoder JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 public class BrowseCategories extends RubbosHttpServlet
38 {
39
40   public int getPoolSize()
41   {
42     return Config.BrowseCategoriesPoolSize;
43   }
44
45   private void closeConnection(PreparedStatement JavaDoc stmt, Connection JavaDoc conn)
46   {
47     try
48     {
49       if (stmt != null)
50         stmt.close(); // close statement
51
}
52     catch (Exception JavaDoc ignore)
53     {
54     }
55
56     try
57     {
58       if (conn != null)
59           releaseConnection(conn);
60     }
61     catch (Exception JavaDoc ignore)
62     {
63     }
64
65   }
66
67   /** Build the html page for the response */
68   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
69       throws IOException JavaDoc, ServletException JavaDoc
70   {
71
72     ServletPrinter sp = null;
73     PreparedStatement JavaDoc stmt = null;
74     Connection JavaDoc conn = null;
75
76     sp = new ServletPrinter(response, "BrowseCategories");
77     sp.printHTMLheader("RUBBoS available categories");
78
79     conn = getConnection();
80
81     ResultSet JavaDoc rs = null;
82     try
83     {
84       stmt = conn.prepareStatement("SELECT * FROM categories");
85       rs = stmt.executeQuery();
86     }
87     catch (Exception JavaDoc e)
88     {
89       sp.printHTML("Failed to execute Query for BrowseCategories: " + e);
90       closeConnection(stmt, conn);
91       return;
92     }
93     try
94     {
95       if (!rs.first())
96       {
97         sp
98             .printHTML("<h2>Sorry, but there is no category available at this time. Database table is empty</h2><br>\n");
99         closeConnection(stmt, conn);
100         return;
101       }
102       else
103         sp.printHTML("<h2>Currently available categories</h2><br>\n");
104
105       int categoryId;
106       String JavaDoc categoryName;
107
108       do
109       {
110         categoryId = rs.getInt("id");
111         categoryName = rs.getString("name");
112         sp
113             .printHTMLHighlighted("<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.BrowseStoriesByCategory?category="
114                 + categoryId
115                 + "&categoryName="
116                 + URLEncoder.encode(categoryName)
117                 + "\">"
118                 + categoryName
119                 + "</a><br>\n");
120       }
121       while (rs.next());
122     }
123     catch (Exception JavaDoc e)
124     {
125       sp.printHTML("Exception getting categories: " + e + "<br>");
126     }
127
128     closeConnection(stmt, conn);
129
130
131     sp.printHTMLfooter();
132
133   }
134
135   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
136       throws IOException JavaDoc, ServletException JavaDoc
137   {
138     doGet(request, response);
139   }
140
141 }
142
Popular Tags