问题:当我希望调用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 回答
问题是你的上下文路径 . B / c路径设置为
Jetty返回一个HTTP
302 Found
,其位置告诉浏览器"get the page from here":然后使用GET检索实际页面 . 将contextPath更改为
/
以查看预期结果: