JSP Hello World on Jetty 9

Running a JSP file on Jetty is quite simple. There is only a trick officially reported to add JSP support to Jetty.

Let’s make an example.

Project Explorer is going to look like this.
jettyPackageExplorer

First, just create a new Java project on Eclipse and this is how main() method looks like.

Main.java

package com;

public class Main {

	public static void main(String[] args) {
		System.out.println("server is starting...");
		HttpServer.getInstance().startHttpServer();

	}

}

HttpServer.java

package com;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;

public class HttpServer {

	private static HttpServer httpServer;
	private Server server = null;

	public static synchronized HttpServer getInstance() {
		if (httpServer == null) {
			httpServer = new HttpServer();
		}
		return httpServer;
	}

	public void startHttpServer() {

		try {
			QueuedThreadPool queuedThreadPool = new QueuedThreadPool();
			queuedThreadPool.setMinThreads(1);
			queuedThreadPool.setMaxThreads(10);
			queuedThreadPool.setIdleTimeout(1000);
			// queuedThreadPool.setMaxStopTimeMs(cm.getHttpThreadPoolMaxStopTime());
			queuedThreadPool.setName("JETTY_SERVER");

			Server server = new Server(queuedThreadPool);

			ServerConnector connector = new ServerConnector(server);
			connector.setPort(8080);

			connector.setIdleTimeout(1000);
			// connector.setLowResourceMaxIdleTime(cm.getHttpConnectorLowResourceMaxIdleTime());
			// connector.setStatsOn(false);

			server.setConnectors(new Connector[] { connector });

			ContextHandlerCollection contexts = new ContextHandlerCollection();

			ServletContextHandler schXCorr = new ServletContextHandler(
					contexts, "afsin", ServletContextHandler.SESSIONS
							| ServletContextHandler.NO_SECURITY);

			WebAppContext webapp = new WebAppContext(contexts, "afsin.war", "/");

			/****** JSP SUPPORT ADD TO JETTY ****/
			org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList
					.setServerDefault(server);
			classlist.addBefore(
					"org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
					"org.eclipse.jetty.annotations.AnnotationConfiguration");
			webapp.setAttribute(
					"org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
					".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/[^/]*taglibs.*\\.jar$");
			/*********************************/

			contexts.setHandlers(new Handler[] { schXCorr, webapp });
			server.setHandler(contexts);

			// server.setSendServerVersion(false);
			// server.setSendDateHeader(false);

			this.server = server;

			server.start();

			System.out.println("server is started!");

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public Server getServer() {
		return server;
	}

}

Utils.java

package com;

public class Utils {

	public static String getTestData() {
		return "TEST";
	}

}

Now just add a new JSP file under /WebContent

HelloWorld.jsp

<%@page import="java.util.Date"%>
<%@page import="com.Utils"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Title</title>
</head>
<body>
	<p>Hello World!</p>

	Test data	 : <%=Utils.getTestData()%>
	<br /> 
	Today 		 : <%=new Date()%>

</body>
</html>

and update web.xml file under /WebContent/WEB-INF

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<display-name>JSP_JETTY</display-name>
	<welcome-file-list>
		<welcome-file>HelloWorld.jsp</welcome-file>
	</welcome-file-list>
</web-app>

Now you can just right click the project and export your .war file and .jar file or you can use Apache Ant or alternative project management tool to build project. Consider that name of your .war file mentioned in HttpServer.java (line.51)

So you are ready to start your server.

You just copy you .war file under $JETTY_HOME/webapps and then type “java -jar start.jar” to start server. Then go to http://your.ip.address:8080/test_jetty (or http://127.0.0.1:8080 or http://localhost:8080, you know what I mean!) which is mentioned in HttpServer.java (line.48)

You should see something like this:


jettysnapshot

Any criticism is welcome!

Source: http://www.eclipse.org/jetty/documentation/current/embedded-examples.html#embedded-webapp-jsp

Tagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: