本文實例講述了JSP針對表單重復提交的處理方法。分享給大家供大家參考,具體如下:
1. 在生成表單時執行如下:
復制代碼 代碼如下:session.setAttribute("forum_add", "forum_add");
2. 提交處理時作如下判斷
if (isRedo(request, "forum_add")) {
//提示重復提交,作相關處理
}
相關函數:
/**
* 判斷是否為重復提交
* 1,檢查Session中是否含有指定名字的屬性
* 2,如果Session中沒有該屬性或者屬性為空,證明已被處理過,判斷為重復提交
* 3,否則,證明是第一次處理,並將屬性從Session中刪除。
* @param key String
*/
private boolean isRedo(HttpServletRequest request, String key) {
String value = (String) request.getSession().getAttribute(key);
if (value == null) {
return true;
}
else {
request.getSession().removeAttribute(key);
return false;
}
}
希望本文所述對大家JSP程序設計有所幫助。