69
import java.io.DataInputStream;
70
import java.io.IOException;
71
import java.io.PrintWriter;
73
import javax.servlet.ServletConfig;
74
import javax.servlet.ServletException;
75
import javax.servlet.ServletOutputStream;
76
import javax.servlet.http.HttpServletRequest;
77
import javax.servlet.http.HttpServletResponse;
79
import org.hsqldb.persist.HsqlProperties;
80
import org.hsqldb.rowio.RowInputBinary;
81
import org.hsqldb.rowio.RowOutputBinary;
83
// fredt@users 20020130 - patch 475586 by wreissen@users
84
// fredt@users 20020328 - patch 1.7.0 by fredt - error trapping
85
// fredt@users 20030630 - patch 1.7.2 - new protocol, persistent sessions
86
// fredt@users 20041112 - patch by Willian Crick - use web_inf directory
89
* Servlet can act as an interface between the client and the database for the
90
* the client / server mode of HSQL Database Engine. It uses the HTTP protocol
91
* for communication. This class is not required if the included HSQLDB
92
* Weberver is used on the server host. But if the host is running a J2EE
93
* application server or a servlet container such as Tomcat, the Servlet class
94
* can be hosted on this server / container to serve external requests from
96
* The remote applet / application should
97
* use the normal JDBC interfaces to connect to the URL of this servlet. An
100
* jdbc:hsqldb:http://localhost.com:8080/servlet/org.hsqldb.Servlet
102
* The database path/name is taken from the servlet engine property:
104
* hsqldb.server.database
107
* If the database is deployed in the WEB-INF directory of the servlet container,
110
* hsqldb.server.use_web-inf_path
112
* should be set "true" in the web.xml file of the servlet container.
113
* In this case, the database path should begin with a "/".
115
* From version 1.7.2 JDBC connections via the HTTP protocol are persistent
116
* in the JDBC sense. The JDBC Connection that is established can support
117
* transactions spanning several Statement calls and real PreparedStatement
118
* calls are supported. This class has been rewritten to support the new
122
* Extensively rewritten for HSQLDB.
124
* @author Thomas Mueller (Hypersonic SQL Group)
126
* @since Hypersonic SQL
128
public class Servlet extends javax.servlet.http.HttpServlet {
130
private static final int BUFFER_SIZE = 256;
131
private String dbType;
132
private String dbPath;
133
private String errorStr;
134
private RowOutputBinary rowOut;
135
private RowInputBinary rowIn;
136
private int iQueries;
144
public void init(ServletConfig config) {
149
rowOut = new RowOutputBinary(BUFFER_SIZE);
150
rowIn = new RowInputBinary(rowOut);
151
} catch (ServletException exp) {
152
log(exp.getMessage());
155
String dbStr = getInitParameter("hsqldb.server.database");
161
// begin WEB-INF patch */
162
String useWebInfStr =
163
getInitParameter("hsqldb.server.use_web-inf_path");
165
if (!dbStr.equals(".") && "true".equalsIgnoreCase(useWebInfStr)) {
166
dbStr = getServletContext().getRealPath("/") + "WEB-INF" + dbStr;
170
HsqlProperties dbURL = DatabaseURL.parseURL(dbStr, false);
172
log("Database filename = " + dbStr);
175
errorStr = "Bad Database name";
177
dbPath = dbURL.getProperty("database");
178
dbType = dbURL.getProperty("connection_type");
182
// loosecannon1@users 1.7.2 patch properties on the JDBC URL
183
DatabaseManager.getDatabase(dbType, dbPath, dbURL);
184
} catch (HsqlException e) {
185
errorStr = e.getMessage();
190
log("Initialization completed.");
193
private static long lModified = 0;
203
protected long getLastModified(HttpServletRequest req) {
205
// this is made so that the cache of the http server is not used
206
// maybe there is some other way
217
* @throws IOException
218
* @throws ServletException
220
public void doGet(HttpServletRequest request,
221
HttpServletResponse response)
222
throws IOException, ServletException {
224
String query = request.getQueryString();
226
if ((query == null) || (query.length() == 0)) {
227
response.setContentType("text/html");
229
// fredt@users 20020130 - patch 1.7.0 by fredt
230
// to avoid caching on the browser
231
response.setHeader("Pragma", "no-cache");
233
PrintWriter out = response.getWriter();
236
"<html><head><title>HSQL Database Engine Servlet</title>");
237
out.println("</head><body><h1>HSQL Database Engine Servlet</h1>");
238
out.println("The servlet is running.<p>");
240
if (errorStr == null) {
241
out.println("The database is also running.<p>");
242
out.println("Database name: " + dbType + dbPath + "<p>");
243
out.println("Queries processed: " + iQueries + "<p>");
245
out.println("<h2>The database is not running!</h2>");
246
out.println("The error message is:<p>");
247
out.println(errorStr);
250
out.println("</body></html>");
261
* @throws IOException
262
* @throws ServletException
264
public void doPost(HttpServletRequest request,
265
HttpServletResponse response)
266
throws IOException, ServletException {
268
synchronized (this) {
269
DataInputStream inStream = null;
270
ServletOutputStream outStream = null;
274
// fredt@users - the servlet container, Resin does not return all
275
// the bytes with one call to input.read(b,0,len) when len > 8192
276
// bytes, the loop in the Result.read() method handles this
277
inStream = new DataInputStream(request.getInputStream());
279
Result resultIn = Result.read(rowIn, inStream);
282
if (resultIn.mode == ResultConstants.SQLCONNECT) {
284
Session session = DatabaseManager.newSession(dbType,
285
dbPath, resultIn.getMainString(),
286
resultIn.getSubString(), null);
288
resultOut = new Result(ResultConstants.UPDATECOUNT);
289
resultOut.sessionID = session.getId();
290
} catch (HsqlException e) {
291
resultOut = new Result(e, null);
294
Session session = DatabaseManager.getSession(dbType,
295
dbPath, resultIn.sessionID);
297
resultOut = session.execute(resultIn);
301
resultOut.write(rowOut);
304
response.setContentType("application/octet-stream");
305
response.setContentLength(rowOut.size());
308
outStream = response.getOutputStream();
310
outStream.write(rowOut.getOutputStream().getBuffer(), 0,
311
rowOut.getOutputStream().size());
314
} catch (HsqlException e) {}
316
if (outStream != null) {
320
if (inStream != null) {
326
// Trace.printSystemOut("Queries processed: "+iQueries+" \n");
69
import java.io.DataInputStream;
70
import java.io.IOException;
71
import java.io.PrintWriter;
73
import javax.servlet.ServletConfig;
74
import javax.servlet.ServletException;
75
import javax.servlet.ServletOutputStream;
76
import javax.servlet.http.HttpServletRequest;
77
import javax.servlet.http.HttpServletResponse;
79
import org.hsqldb.persist.HsqlProperties;
80
import org.hsqldb.rowio.RowInputBinary;
81
import org.hsqldb.rowio.RowOutputBinary;
83
// fredt@users 20020130 - patch 475586 by wreissen@users
84
// fredt@users 20020328 - patch 1.7.0 by fredt - error trapping
85
// fredt@users 20030630 - patch 1.7.2 - new protocol, persistent sessions
86
// fredt@users 20041112 - patch by Willian Crick - use web_inf directory
89
* Servlet can act as an interface between the client and the database for the
90
* the client / server mode of HSQL Database Engine. It uses the HTTP protocol
91
* for communication. This class is not required if the included HSQLDB
92
* Weberver is used on the server host. But if the host is running a J2EE
93
* application server or a servlet container such as Tomcat, the Servlet class
94
* can be hosted on this server / container to serve external requests from
96
* The remote applet / application should
97
* use the normal JDBC interfaces to connect to the URL of this servlet. An
100
* jdbc:hsqldb:http://localhost.com:8080/servlet/org.hsqldb.Servlet
102
* The database path/name is taken from the servlet engine property:
104
* hsqldb.server.database
107
* If the database is deployed in the WEB-INF directory of the servlet container,
110
* hsqldb.server.use_web-inf_path
112
* should be set "true" in the web.xml file of the servlet container.
113
* In this case, the database path should begin with a "/".
115
* From version 1.7.2 JDBC connections via the HTTP protocol are persistent
116
* in the JDBC sense. The JDBC Connection that is established can support
117
* transactions spanning several Statement calls and real PreparedStatement
118
* calls are supported. This class has been rewritten to support the new
122
* Extensively rewritten for HSQLDB.
124
* @author Thomas Mueller (Hypersonic SQL Group)
126
* @since Hypersonic SQL
128
public class Servlet extends javax.servlet.http.HttpServlet {
130
private static final int BUFFER_SIZE = 256;
131
private String dbType;
132
private String dbPath;
133
private String errorStr;
134
private RowOutputBinary rowOut;
135
private RowInputBinary rowIn;
136
private int iQueries;
144
public void init(ServletConfig config) {
149
rowOut = new RowOutputBinary(BUFFER_SIZE);
150
rowIn = new RowInputBinary(rowOut);
151
} catch (ServletException exp) {
155
String dbStr = getInitParameter("hsqldb.server.database");
161
// begin WEB-INF patch */
162
String useWebInfStr =
163
getInitParameter("hsqldb.server.use_web-inf_path");
165
if (!dbStr.equals(".") && "true".equalsIgnoreCase(useWebInfStr)) {
166
dbStr = getServletContext().getRealPath("/") + "WEB-INF" + dbStr;
170
HsqlProperties dbURL = DatabaseURL.parseURL(dbStr, false);
172
log("Database filename = " + dbStr);
175
errorStr = "Bad Database name";
177
dbPath = dbURL.getProperty("database");
178
dbType = dbURL.getProperty("connection_type");
182
// loosecannon1@users 1.7.2 patch properties on the JDBC URL
183
DatabaseManager.getDatabase(dbType, dbPath, dbURL);
184
} catch (HsqlException e) {
185
errorStr = e.getMessage();
190
log("Initialization completed.");
193
private static long lModified = 0;
203
protected long getLastModified(HttpServletRequest req) {
205
// this is made so that the cache of the http server is not used
206
// maybe there is some other way
217
* @throws IOException
218
* @throws ServletException
220
public void doGet(HttpServletRequest request,
221
HttpServletResponse response)
222
throws IOException, ServletException {
224
String query = request.getQueryString();
226
if ((query == null) || (query.length() == 0)) {
227
response.setContentType("text/html");
229
// fredt@users 20020130 - patch 1.7.0 by fredt
230
// to avoid caching on the browser
231
response.setHeader("Pragma", "no-cache");
233
PrintWriter out = response.getWriter();
236
"<html><head><title>HSQL Database Engine Servlet</title>");
237
out.println("</head><body><h1>HSQL Database Engine Servlet</h1>");
238
out.println("The servlet is running.<p>");
240
if (errorStr == null) {
241
out.println("The database is also running.<p>");
242
out.println("Database name: " + dbType + dbPath + "<p>");
243
out.println("Queries processed: " + iQueries + "<p>");
245
out.println("<h2>The database is not running!</h2>");
246
out.println("The error message is:<p>");
247
out.println(errorStr);
250
out.println("</body></html>");
261
* @throws IOException
262
* @throws ServletException
264
public void doPost(HttpServletRequest request,
265
HttpServletResponse response)
266
throws IOException, ServletException {
268
synchronized (this) {
269
DataInputStream inStream = null;
270
ServletOutputStream outStream = null;
274
// fredt@users - the servlet container, Resin does not return all
275
// the bytes with one call to input.read(b,0,len) when len > 8192
276
// bytes, the loop in the Result.read() method handles this
277
inStream = new DataInputStream(request.getInputStream());
279
Result resultIn = Result.read(rowIn, inStream);
282
if (resultIn.mode == ResultConstants.SQLCONNECT) {
284
Session session = DatabaseManager.newSession(dbType,
285
dbPath, resultIn.getMainString(),
286
resultIn.getSubString(), null);
288
resultOut = new Result(ResultConstants.UPDATECOUNT);
289
resultOut.sessionID = session.getId();
290
} catch (HsqlException e) {
291
resultOut = new Result(e, null);
294
int dbId = resultIn.databaseID;
295
int sessionId = resultIn.sessionID;
296
Session session = DatabaseManager.getSession(dbId,
299
resultOut = session.execute(resultIn);
303
resultOut.write(rowOut);
306
response.setContentType("application/octet-stream");
307
response.setContentLength(rowOut.size());
310
outStream = response.getOutputStream();
312
outStream.write(rowOut.getOutputStream().getBuffer(), 0,
313
rowOut.getOutputStream().size());
316
} catch (HsqlException e) {}
318
if (outStream != null) {
322
if (inStream != null) {
328
// Trace.printSystemOut("Queries processed: "+iQueries+" \n");