2012-04-04 71 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("/"); 
相关问题