当页面刷新时如何删除getParameter()的值

4

如何在使用F5/刷新页面时删除参数值?期望的结果是,当B.jsp将状态返回给A.jsp时,如果用户按下刷新页面按钮,我希望将该值设置为空。

A.jsp:

<%String VALUE = request.getParameter("STATUS");%>

B.jsp:

<%send.responseRedirect("A.jsp?STATUS="Y");%>

你需要使用会话属性来存储已经收到参数的事实和/或使用Servlets。 - Gaël J
2个回答

2
当html页面被提交到一个servlet/jsp或其他任何类型的web资源时,它包含一些头信息。这些头信息中包含了请求的参数等内容。当用户在浏览器上点击刷新/F5时,浏览器会重新提交之前相同的头信息。因此,这个问题是与浏览器相关的。
现在,有很多种方法来解决这个问题。其中一种方法是将当前值存储在会话变量中,当请求参数与会话属性值相同时,则可以将其视为刷新操作。
请考虑以下newjsp.jsp代码:
String param = request.getParameter("param");
if (session.getAttribute("PARAM") == null) {
    out.print("This is a NEW request");
    session.setAttribute("PARAM", request.getParameter("param"));
} else if (session.getAttribute("PARAM").toString().equalsIgnoreCase(param)) {
    out.print("This is a REFRESH");
    session.removeAttribute("PARAM");
} else {
    out.print("This is a NEW request");
    session.setAttribute("PARAM", request.getParameter("param"));
}

使用“newjsp.jsp?param=xyz123”调用它,然后尝试刷新。


1
如果您的网页上没有使用大型图片,您也可以使用以下代码来移除缓存:
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接