嵌入式Jetty doGet调用,预计doPost

时间:2012-04-04 17:11:09

标签: java jetty

问题:当我希望调用doPost时,只会调用doGet。

我有一个嵌入式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

启动服务器后,打开以下页面(位于“public”文件夹,并通过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>

当我按下Submit按钮时,我希望调用我的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(以及一般的网络技术)对我来说是全新的。我一直在梳理Jetty示例,但似乎无法通过doPost方法获得实际获取的POST。

感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分: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("/");