gpt4 book ai didi

java - HttpServletRequest.getParameter 丢失参数

转载 作者:行者123 更新时间:2023-11-30 03:38:36 27 4
gpt4 key购买 nike

我很困惑,当我确实在请求中包含该参数时,HttpServletRequest.getParameter 有时会返回 null。

测试程序如下:

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 中的/hello

在tomcat或jetty中启动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传递给Thread。有时它会在请求完成之前执行,有时会稍后执行。因此,有时你得到参数,有时它们是null

避免在 doGetdoPost 之外处理 HttpServletRequest。您应该将原始请求中的信息复制到单独的数据结构中以供以后处理。

例如:

public class HelloServlet extends HttpServlet {
private static Executor logExecutor = Executors.newFixedThreadPool(5);

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String data = String.format("a=%s b=%s c=%s",
req.getParameter("a"), req.getParameter("b"),
req.getParameter("c"));
logExecutor.execute(new Task(data));
}
}

class Task implements Runnable {
private String data;

public Task(String data) {
this.data = data;
}

@Override
public void run() {
System.out.println(data);
}
}

编辑:

摘自Java Servlet Specification :

3.11 Lifetime of the Request Object

Each request object is valid only within the scope of a servlet’s service method, or within the scope of a filter’s doFilter method, unless the asynchronous processing is enabled for the component and the startAsync method is invoked on the request object. In the case where asynchronous processing occurs, the request object remains valid until complete is invoked on the AsyncContext. Containers commonly recycle request objects in order to avoid the performance overhead of request object creation. The developer must be aware that maintaining references to request objects for which startAsync has not been called outside the scope described above is not recommended as it may have indeterminate results.

关于java - HttpServletRequest.getParameter 丢失参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27290524/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com