维护Servlet请求属性状态

问题描述 投票:0回答:1

考虑一个 JSP 页面,它具有以下值。

String value = null;
if(request.getParamter("VALUE")!=null)
{
  value = request.getParamter("VALUE");
} 
/// other logics
if(true)
{
request.setAttribute("VALUE",value) 
SomeClass.method(request);
}

现在只有当特定请求在请求中有价值并且它才会被分配 如果没有该值的其他请求在调用 SomeClass 方法之前将其设置为空,那么我将不会在 SomeClass 方法中拥有该值。 如何在不使用会话和 cookie 的情况下解决这个问题,因为如果使用会话或 cookie,我可能会获得并发值,这可能会导致问题。 使用 static 可能会导致不同线程情况下的问题。 Tomcat 有没有解决这个问题的方法。

java tomcat servlets
1个回答
0
投票

我认为您需要将 value 设置为局部变量,这样它就不会被其他请求更新。一旦它被设置为唯一请求中的属性,它就不能被其他并发请求更新。

if(request.getParamter("VALUE") != null)
{
  String value = request.getParamter("VALUE");
  request.setAttribute("VALUE",value) 
} 
/// other logics
if(true)
{
SomeClass.method(request);
}
© www.soinside.com 2019 - 2024. All rights reserved.