Back to the Main Page
I was using Jetty for my http services, but the version I was using was getting old (9.4.57) and if
I wanted to use the later versions I needed to use Maven. I have very strong feelings against using Maven.
<Controversy>
I can't see the point of a library version of webstart. You should simply download the version you want, manually, and use it.
You should be in complete control of what libraries you are using and libraries should be very open about what libraries they need.
It's not like you need to avoid deploying large applications and are restricted to a couple of megabytes. Applications tend to only be a couple of megabytes
and I've got 15 year old thumb drives that can handle that. Downloading the whole thing from a website isn't an issue these days either unless you are still on a dial-up connection.
I feel it was a solution to a problem that had disappeared by the time it was created. You just need to ensure your documentation is up to snuff;
listing all of the libraries your library requires, not just directly but completely.
You know what they are, you don't need a pom (by that I mean a pom.xml file not an Englishman) to tell you.
Documentation is as important as code imho!
Having said that there are moves afoot to make it easier for beginners to use Java.
That public static void main(String[] arg){} is too complicated for first time users, despite it being the fundamental part of Java. Having a starting point is useful,
I think, and it's not the only language to do this.
IE this:
is easier. Arguably it is, but the first example demonstrates the language, whereas the second does not. </Controversy>
Also the later versions of Jetty are huge, and full of stuff I do not need.
So what is the alternative to Jetty?
Without Jetty? Write my own! The java built in versions of HTTP objects
(com.sun.net.httpserver.Headers, com.sun.net.httpserver.HttpExchange, com.sun.net.httpserver.HttpHandler, etc.)
are sufficient for me to create a simple, low traffic web site.
However, the basic HttpHandler object can only handle one request at a time, so I needed to expand it.
Now the HttpHandler will fork off another process and return immediately.
What does that mean?
The main class does the configuration, and I created an abstract class for the handlers (MyHandler) that implements the HttpHandler
interface and has an abstract method called handleRequest.
Instead of using the handle(HttpExchange) method to handle the http conversation I sent it off to another thread:
This is copied from my full web server which contains all sorts of handlers.
I've stripped it back to the bare essentials. If you need help contact me via source forge.
All these can be found in the archive file (Source Archive) including the ByteArrayBuilder.
The new thread gets fired up and handles the http conversation while the initial handler returns from the Handle method: ByteArrayBuilder is used quite a bit through out these classes.
I've added the code I use to extract parameters from the post/get actions, including multi part posts.
Files committed get added to the UPLOADS_DIR folder and the properties will include a FIELD_NAME_FileName property.
The abstract class would look something like this:
The HELLO WORLD handler is simply:
A static file handler would look something like this:
For HTTPS servers see How to Get A Certificate From Let's Encypt.
The configurator would look something like this:
So the main class looks something like this:
This will run forever (or crash trying). In my version I've set up a control thread to look for a specific file to stop this process so you just touch a file name to stop (not included here).
You can always use the kill command to do it, or, if you've set up the service on a *nix box, sudo service webd stop|start|restart.
This is my script that allows you to register a service for serviced (the comments are required). Put it in /etc/init.d.
#!/bin/bash
### BEGIN INIT INFO
# Provides: webd
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: Starts the website
### END INIT INFO
start() {
cd /usr/local/web/
java -jar MyWeb.jar &
}
stop() {
touch /usr/local/web/exitplease
}
help() {
cd /usr/local/web/
java -jar MyWeb.jar -?
}
if [ "$USER" != "root" ]
then
echo "Web server can only be stopped/started by root."
exit 2
fi
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 10
start
;;
?)
help
;;
*)
echo "Usage /etc/init.d/webd {start|stop|restart|?}"
;;
esac
Back to the Main Page
I have created this page so I can keep track of how I did things.
Viewed times.