jetty is a lightweight web app server (e.g. for java .war files), a touch less heavy than tomcat but also embeddable
You can get it from Eclipse http://wiki.eclipse.org/Jetty or a more Enterprise edition from CodeHaus.
http://download.eclipse.org/jetty/ will list the correct jetty version you will want to pick
#!/bin/bash
JETTY_VERSION=8.1.2.v20120302
wget http://download.eclipse.org/jetty/$JETTY_VERSION/dist/jetty-distribution-$JETTY_VERSION.tar.gz
cd jetty-distribution-8.1.2.v20120302
(the README has lots of variations and options)
java -jar start.jar
(by default it will extract the webapps/test.jar into /tmp/jetty-0.0.0.0-8080-test.war-_-any-/webapp )
(java uses about 80MB of RAM)
netstat --inet -an
Browsing to http://localhost:8080 gives a good overview of the many ways to use Jetty + webapps
Control + C stops the web server application (graceful shutdown and cleans up /tmp)
java -jar start.jar --list-options (full list of options that can be combined)
i.e. super slim but maybe not so useful or needs more custom configs:
java -jar start.jar --ini OPTIONS=Server,websocket
2012-02-29 22:06:43.864:INFO:oejs.Server:jetty-8.1.2.v20120302
2012-02-29 22:06:43.920:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
If you remove the test.war (and ensure you refresh/reload your browser you'll see the app has been removed)
/jetty-distribution-8.1.2.v20120302/webapps/Hi.war (see below) and it runs java using 50MB of RAM
java -jar start.jar --ini OPTIONS=Server,websocket etc/jetty.xml etc/jetty-deploy.xml etc/jetty-webapps.xml
java -X (non standard options)
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size
-Xmx128m and MaxPermSize=128m (would at most use 256m)
For server side Java apps it's good practice to set -Xms and -Xmx to the same value
java -X -Xms20m -Xmx20m -jar start.jar --ini OPTIONS=Server,websocket etc/jetty.xml etc/jetty-deploy.xml etc/jetty-webapps.xml
(uses about 40MB of RAM)
(http://www.caucho.com/resin-3.0/performance/jvm-tuning.xtp)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class Hi extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType( "text/html" ); // MIME type
PrintWriter out = null;
try{ out = response.getWriter();
}
catch( Exception e )
{ System.err.println( "Unable to create a PrintWriter" );
e.printStackTrace();
System.exit( 1 );
}
outputXHTMLHeader( "Hi" , out );
out.println( "<body>" );
out.println( "Hi" );
out.println( "</body></html>" );
}
private static void outputXHTMLHeader( String title , PrintWriter servletresponse )
{
servletresponse.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
servletresponse.println( "<html xmlns=\"http://www.w3.org/1999/xhtml\">" );
servletresponse.println( "<head><title>" + title + "</title>" );
servletresponse.println( "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />" );
servletresponse.println( "</head>" );
} //end outputXHTMLHeader
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ doGet( request, response);
}
}