我很困惑,HttpServletRequest.getParameter有時會返回null,而我真的在請求中包含參數。HttpServletRequest.getParameter丟失參數
測試下面的程序:
HelloServlet.java:
public class HelloServlet extends HttpServlet {
private static Executor logExecutor = Executors.newFixedThreadPool(5);
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
logExecutor.execute(new Task(req));
}
}
class Task implements Runnable {
private HttpServletRequest req;
public Task(HttpServletRequest req) {
this.req = req;
}
@Override
public void run() {
System.out.println(String.format("a=%s b=%s c=%s",
req.getParameter("a"), req.getParameter("b"),
req.getParameter("c")));
}
}
地圖這個servlet來/你好在web.xml
開始在Tomcat或碼頭servlet時,啓動與請求shell腳本:
#/bin/sh
for i in {1..100}
do
curl -i -X GET "http://localhost:8080/hello?a=a&b=b&c=c"
done
服務器日誌顯示某些參數在某些請求可能爲空,並且此情況發生時沒有常規模式。如:
a=a b=b c=c
a=a b=b c=c
a=null b=null c=null
a=null b=null c=null
a=null b=null c=null
a=null b=b c=c
a=null b=null c=c
a=a b=b c=c
a=a b=b c=c
a=a b=b c=c
a=a b=b c=c
a=null b=null c=null
a=a b=b c=c
a=null b=b c=c
我發現原因是我無法爲我的執行程序持有HttpServletRequest實例。 所以我想知道原因!爲什麼我在一個請求中持有HttpServletRequest實例的操作可能會影響其他請求。
我很好奇爲什麼一個HttpServletRequest的實例的值可以由其他請求的影響。 – terry 2014-12-05 07:38:46
我並不是說HttpServletRequest可能會受到另一個的影響。但是一旦請求完成(響應發送給客戶端),應用程序服務器(tomcat,jetty ...)就可以清除它的數據。 – 2014-12-05 08:22:16