1

問題: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>

[送信]ボタンを押すと、サーブレットの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(および一般的なWebテクノロジー)は私にとってまったく新しいものです。私はJettyの例をくまなく調べてきましたが、実際にdoPostメソッドによって取得されるPOSTを取得できないようです。

助けに感謝します。前もって感謝します。

4

1 に答える 1

4

問題はコンテキストパスです。B/cパスは次のように設定されます

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

Jettyは302 Found、ブラウザに「ここからページを取得する」ように指示する場所でHTTPを 返します。

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("/");
于 2012-04-04T19:42:12.460 に答える