2012-04-04 56 views
1

問題:只有在doPet被調用時纔會調用doGet。Embedded Jetty doGet調用,預計doPost

我有我開始作爲一個嵌入式Jetty服務器如下:

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

ResourceHandler rh = new ResourceHandler(); 
rh.setResrouceBase("C:\\public"); 

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

server.setHandler(hl); 

//server.start() follows 

我啓動服務器後,我打開如下頁面(駐留在「公共」文件夾,並通過http://localhost:8080/test.html打開):

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

當我按提交按鈕,我期待我的servlet的doPost方法被調用,但doGet似乎被調用。 MyServlet類(延伸的HttpServlet)包含:

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()); 
} 

我從來沒有得到過的doPost打印,距離的doGet一個(上提交按鈕按下)。

顯然,碼頭(和網絡技術在一般)是全新的我。我一直在梳理Jetty的例子,但似乎無法獲得POST實際上被doPost方法拾取。

感謝任何幫助。提前致謝。

回答

4

問題是您的上下文路徑。 B/C中的路徑設置爲

myContext.setContextPath("/Test.do"); 

Jetty是與告訴瀏覽器的位置返回一個HTTP 302 Found「從這裏得到的頁面」:

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 

實際的頁面,然後用GET進行檢索。將contextPath更改爲/以查看您的預期結果:

myContext.setContextPath("/");