Reputation: 3507
I am tying to run a servlet using Jetty but only by using java code(embedded jetty). Here are my two classes :
ExampleServer.java :
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.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
/**
* Created by Administrator on 7/8/14.
*/
public class ExampleServer {
public static void main(String[] args) throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.setConnectors(new Connector[]{connector});
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/hello");
context.addServlet(HelloServlet.class, "/");
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[]{context, new DefaultHandler()});
server.setHandler(handlers);
server.start();
server.join();
}
}
HelloServlet.java :
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by Administrator on 7/8/14.
*/
public class HelloServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Hello from HelloServlet</h1>");
response.getWriter().println("session=" + request.getSession(true).getId());
}
}
Now when I am trying to access http://localhost:8080/hello
I get the following error :
HTTP ERROR: 500
Problem accessing /hello/. Reason:
java.lang.IllegalStateException: No SessionManager
Any ideas on how to fix that? Thank you.
Upvotes: 0
Views: 1278
Reputation: 201537
You seem to have missed a step,
context.addServlet(HelloServlet.class, "/");
context.setSessionHandler(new org.eclipse.jetty.server.session.SessionHandler());
Normally, the configuration file is used to set that by default (or to use JDBCSessionManager for a cluster). Also, you only need this because you called request.getSession(true).getId()
Upvotes: 1