jsp页面怎么防止刷新页面又提交表单
答案:3 悬赏:80 手机版
解决时间 2021-12-02 04:56
- 提问者网友:咪咪
- 2021-12-01 18:41
jsp页面怎么防止刷新页面又提交表单
最佳答案
- 五星知识达人网友:旧脸谱
- 2021-12-01 19:38
JSP刷新页面表单重复提交问题解决办法,使用sessionID和时间戳作为标识,关键代码如下:
public class SswpdjAction extends BaseAction{
public String execute(){
................
//设置标识
this.setSessionToken();
//转到添加页面
return "toAdd";
}
public String reSave(){
if(this.token != null && this.token.equals(this.getSessionToken())){
this.setSessionToken();
..............
return "toAdd";
}else{
printWriter out = null;
try{
httpServletResponse.setContentType("text/html;charset=UTF-8");
out = httpServletResponse.getWriter();
out.println("");
out.flush();
}catch(IOException e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
}
return null;
}
}
public class BaseAction extends ActionSupport{
protected String token;
public String getToken(){
return token;
}
public void setToken(String token){
this.token = token;
}
public String getSessionToken(){
if(null != httpSession.getAttribute("Token")){
return httpSession.getAttribute("Token");
}else{
return null;
}
}
public void setSessionToken(){
String flag = useMd5(httpSession.getId() + System.currentTimeMillis());
httpSession.setAttribute("Token", flag);
httpServletRequest.setAttribute("SessionToken", flag);
}
private String useMd5(String str){
byte[] bs = str.getBytes();
String result = null;
try{
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(bs);
result = md5.digest().toString();
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}finally{
}
return result;
}
}JSP页面设置标识隐藏域:
public class SswpdjAction extends BaseAction{
public String execute(){
................
//设置标识
this.setSessionToken();
//转到添加页面
return "toAdd";
}
public String reSave(){
if(this.token != null && this.token.equals(this.getSessionToken())){
this.setSessionToken();
..............
return "toAdd";
}else{
printWriter out = null;
try{
httpServletResponse.setContentType("text/html;charset=UTF-8");
out = httpServletResponse.getWriter();
out.println("");
out.flush();
}catch(IOException e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
}
return null;
}
}
public class BaseAction extends ActionSupport{
protected String token;
public String getToken(){
return token;
}
public void setToken(String token){
this.token = token;
}
public String getSessionToken(){
if(null != httpSession.getAttribute("Token")){
return httpSession.getAttribute("Token");
}else{
return null;
}
}
public void setSessionToken(){
String flag = useMd5(httpSession.getId() + System.currentTimeMillis());
httpSession.setAttribute("Token", flag);
httpServletRequest.setAttribute("SessionToken", flag);
}
private String useMd5(String str){
byte[] bs = str.getBytes();
String result = null;
try{
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(bs);
result = md5.digest().toString();
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}finally{
}
return result;
}
}JSP页面设置标识隐藏域:
全部回答
- 1楼网友:走死在岁月里
- 2021-12-01 20:33
添加验证码,
- 2楼网友:行路难
- 2021-12-01 20:04
很久以前在网上找的,你试试吧
用JavaScript解决
在表单中写一个隐藏域
如果第一次提交,就改变 value="true" ;
再次提交时候,可以用JS 在客户端判断hidde值
var resubmit = false;
function checkCFSubmit()
{
if (resubmit )
{
return false;
}
else
{
resubmit = true;
return true;
}
然后在
用JavaScript解决
在表单中写一个隐藏域
如果第一次提交,就改变 value="true" ;
再次提交时候,可以用JS 在客户端判断hidde值
var resubmit = false;
function checkCFSubmit()
{
if (resubmit )
{
return false;
}
else
{
resubmit = true;
return true;
}
然后在
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯