読者です 読者をやめる 読者になる 読者になる

jetty7.4.2 sample

webのどっかにあるサンプルを動かした時、NullPointerExceptionが起こって訳わからん。
ってなったので、とりあえず動いたソースをはる。

import org.eclipse.jetty.server.*;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class jettyServer {
	public static void main(String[] args) throws Exception {
		Server server = new Server(8080);

		ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
		context.setContextPath("/");
		server.setHandler(context);

		context.addServlet(new ServletHolder(new HelloServlet()), "/*");
		context.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")), "/it/*");
		context.addServlet(new ServletHolder(new HelloServlet("Bonjour le Monde")), "/fr/*");

		server.start();
		server.join();
	}
}
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String greeting = "Hello World";

	public HelloServlet() {
	}

	public HelloServlet(String greeting) {
		this.greeting = greeting;
	}

	protected void doGet(HttpServletRequest request,HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.setStatus(HttpServletResponse.SC_OK);
		response.getWriter().println("<h1>" + greeting + "</h1>");
		response.getWriter().println("session=" + request.getSession(true).getId());
	}
}

参照:http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty