[英]request.setAttribute is not overriding with new valuie in servlet
我有接收下面的HttpServletRequest的servlet部分
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (request.getParameter("STATE").equalsIgnoreCase("TX")) {
request.setAttribute("STATE", "IL");
}
System.out.println("Printing " + request.getParameter("STATE"));
}
我写的代码是当我将STATE传递为'TX'时,它在if块中进入,并且覆盖状态为IL。
但是新的价值并没有超越,我总是在得到TX。 收到TX请求后,我需要用IL替换TX。
请帮我
首先,根据您要设置的参数值,使用request.getParmater(“ TX”)在if块内部以及在if块外部再次设置request.setAttribute(“ STATE”,“ IL”),您将再次使用request.getParameter( “州”)。
注意:这里您没有根据参数名称覆盖您,而是在请求中设置了some属性。 请求参数是不可变的,您无法更改
在您的if条件中,您正在设置/覆盖属性,并且正在打印参数,因此不会获得更新值。
您无法从HttpServletRequest更改请求参数,因此需要使用属性来修改请求。
请更改您的代码为
if (request.getParameter("STATE") != null && request.getParameter("STATE").equalsIgnoreCase("TX")) {
// If request contains TX, then add updated STATE in attribute
request.setAttribute("STATE", "IL");
} else {
// If request doesn't contains or contains other than TX then add previous status in attribute
request.setAttribute("STATE", request.getParameter("STATE"));
}
// Now we are using attribute instead of parameter
System.out.println("Printing " + request.getAttribute("STATE"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.