The {@link WebServer} is a minimal HTTP server, that might be usedas an embedded web server.
Use of the {@link WebServer} has grown very popular amongst usersof Apache XML-RPC. Why this is the case, can hardly be explained, because the {@link WebServer} is at best a workaround, compared tofull blown servlet engines like Tomcat or Jetty. For example, under heavy load it will almost definitely be slower than a real servlet engine, because it does neither support proper keepalive (multiple requests per physical connection) nor chunked mode (in other words, it cannot stream requests).
If you still insist in using the {@link WebServer}, it is recommended to use its subclass, the {@link ServletWebServer} instead,which offers a minimal subset of the servlet API. In other words, you keep yourself the option to migrate to a real servlet engine later.
Use of the {@link WebServer} goes roughly like this: First of all,create a property file (for example "MyHandlers.properties") and add it to your jar file. The property keys are handler names and the property values are the handler classes. Once that is done, create an instance of WebServer:
final int portNumber = 8088; final String propertyFile = "MyHandler.properties"; PropertyHandlerMapping mapping = new PropertyHandlerMapping(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); mapping.load(cl, propertyFile); WebServer webServer = new WebServer(port); XmlRpcServerConfigImpl config = new XmlRpcServerConfigImpl(); XmlRpcServer server = server.getXmlRpcServer(); server.setConfig(config); server.setHandlerMapping(mapping); server.start();
WebServer has two distinct functions:
The primary function is to allow client/server access to HSQLDB databases via the HTTP protocol. This protocol is less efficient than the HSQL protocol used by the Server class and should be used only in situations where sandboxes or firewalls between the client and the server do not allow the use of the HSQL protocol. One example is client/server access by an applet running in browsers on remote hosts and accessing the database engine on the HTTP server from which the applet originated. From version 1.7.2, HTTP database connections are persistent and support transactions. Similar to HSQL connections, they should be explicitly closed to free the server resources.
The secondary function of WebServer is to act as a simple general purpose HTTP server. It is aimed to support the minimum requirements set out by the HTTP/1.0 standard. The HEAD and GET methods can be used to query and retreive static files from the HTTP server.
Both the database server and HTTP server functions of WebServer can be configured with the webserver.properties file. It contains entries for the database server similar to those for the HSQL protocol Server class. In addition, a list mapping different file endings to their mime types may be included in this file. (fredt@users)
From the command line, the options are as follows:
+----------------+-------------+----------+------------------------------+ | OPTION | TYPE | DEFAULT | DESCRIPTION | +----------------+-------------+----------+------------------------------| | -? | -- | -- | prints this message | | -address | name|number | any | server inet address | | -port | number | 80 | port at which server listens | | -database.i | [type]spec | 0=test | path of database i | | -dbname.i | alias | -- | url alias for database i | | -silent | true|false | true | false => display all queries | | -trace | true|false | false | display JDBC trace messages | | -no_system_exit| true|false | false | do not issue System.exit() | +----------------+-------------+----------+------------------------------+Example of the webserver.properties file:
server.port=80 server.database.0=test server.dbname.0=... ... server.database.n=... server.dbname.n=... server.silent=true .htm=text/html .html=text/html .txt=text/plain .gif=image/gif .class=application/octet-stream .jpg=image/jpeg .jgep=image/jpeg .zip=application/x-zip-compressed
WebServer has two distinct functions:
The primary function is to allow client/server access to HSQLDB databases via the HTTP protocol. This protocol is less efficient than the HSQL protocol used by the Server class and should be used only in situations where sandboxes or firewalls between the client and the server do not allow the use of the HSQL protocol. One example is client/server access by an applet running in browsers on remote hosts and accessing the database engine on the HTTP server from which the applet originated. From version 1.7.2, HTTP database connections are persistent and support transactions. Similar to HSQL connections, they should be explicitly closed to free the server resources.
The secondary function of WebServer is to act as a simple general purpose HTTP server. It is aimed to support the minimum requirements set out by the HTTP/1.0 standard. The HEAD and GET methods can be used to query and retreive static files from the HTTP server.
Both the database server and HTTP server functions of WebServer can be configured with the webserver.properties file. It contains entries for the database server similar to those for the HSQL protocol Server class. In addition, a list mapping different file endings to their mime types may be included in this file. (fredt@users)
From the command line, the options are as follows:
+-----------------+-------------+----------+------------------------------+ | OPTION | TYPE | DEFAULT | DESCRIPTION | +-----------------+-------------+----------+------------------------------| | --help | | | prints this message | | --address | name|number | any | server inet address | | --port | number | 80 | port at which server listens | | --database.i | [type]spec | 0=test | path of database i | | --dbname.i | alias | | url alias for database i | | --silent | true|false | true | false => display all queries | | --trace | true|false | false | display JDBC trace messages | | --no_system_exit| true|false | false | do not issue System.exit() | +-----------------+-------------+----------+------------------------------+Example of the webserver.properties file:
server.port=80 server.database.0=test server.dbname.0=... ... server.database.n=... server.dbname.n=... server.silent=true .htm=text/html .html=text/html .txt=text/plain .gif=image/gif .class=application/octet-stream .jpg=image/jpeg .jgep=image/jpeg .zip=application/x-zip-compressed
Configures an event based webserver.
To create an instance, use {@link WebServers#createWebServer(int)}.
As with many of the interfaces in webbitserver, setter style methods return a reference to this, to allow for simple initialization using method chaining.
class HelloWorldHandler implements HttpHandler { void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) { response.header("Content-Type", "text/html") .content("Hello World") .end(); } } WebServer webServer = WebServers.createWebServer(8080) .add(new HelloWorldHandler()) .start(); print("Point your browser to " + webServer.getUri());
WebServer webServer = WebServers.createWebServer(8080) .add(new StaticFileHandler("./wwwdata")) .start();@author Joe Walnes @see WebServers @see HttpHandler @see WebSocketConnection @see EventSourceConnection
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|