Embedded Jetty doGet called, expected doPost

Go To StackoverFlow.com


Problem: Only doGet gets called when I expect doPost to be called.

I have an embedded Jetty server that I start as follows:

server = new Server(8080);
ServletContextHandler myContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
myContext.addServlet(new ServletHolder(new MyServlet()), "/*");

ResourceHandler rh = new ResourceHandler();

HandlerList hl = new HandlerList();
hl.setHandlers(new Handler[]{rh, myContext});


//server.start() follows

After I start the server, I open up the following page (resides in "public" folder, and opened via http://localhost:8080/test.html):

<head><title>Test Page</title></head>
<p>Test for Post.</p>
<form method="POST" action="Test.do"/>
<input name="field" type="text" />
<input type="submit" value="Submit" />

When I press the Submit button, I expect my servlet's doPost method to be called, however doGet seems to be getting called instead. MyServlet class (extends HttpServlet) contains:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
  System.out.println("   doGet called with URI: " + request.getRequestURI());

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
  System.out.println("   doPost called with URI: " + request.getRequestURI());

I never get the doPost print, just the one from doGet (on Submit button press).

Obviously, Jetty (and web technology in general) is brand new to me. I've been combing through the Jetty examples, but just can't seem to get a POST to actually get picked up by the doPost method.

Appreciate any help. Thanks in advance.

2012-04-04 17:11
by John


The problem is your context path. B/c the path is set as


Jetty is returning an HTTP 302 Found with a location that tells the browser "get the page from here":

HTTP/1.1 302 Found
Location: http://localhost:8080/test.do/
Server: Jetty(7.0.0.M2)
Content-Length: 0
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Date: Wed, 04 Apr 2012 19:32:01 GMT

The actual page is then retrieved with a GET. Change the contextPath to / to see your expected results:

2012-04-04 19:42
by Tim Jones