java 防止ajax重复提交_JSP使用自定义标签防止表单重复提交的方法

本文实例讲述了JSP使用自定义标签防止表单重复提交的方法。分享给大家供大家参考。具体如下:

1. 编写servelt:

package cn.itcast.apsliyuan.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class LoginServet extends HttpServlet {

/**

* 反序列化

*/

private static final long serialVersionUID = 4960707156159691747L;

@Override

public void doPost(HttpServletRequest requset,

HttpServletResponse response) throws ServletException, IOException {

// 获得输入框中的值

String token = requset.getParameter("tokenName");

// 获得session中的值

String sessionToken = (String) requset.getSession().getAttribute(

"token");

if (token.equals(sessionToken)) {

response.getWriter().print("保存成功");

requset.getSession().removeAttribute("token");//为了保证session中的值不重复,所以移除

} else {

response.getWriter().print("对不起不能重复提交");

}

}

}

2. 编写自定义标签的类:

package cn.itcast.apsliyuan.tag;

import java.io.IOException;

import java.util.UUID;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.TagSupport;

public class LoginTokenTag extends TagSupport{

/**

* 凡序列化

*/

private static final long serialVersionUID = -1815168785364991254L;

@Override

public int doStartTag() throws JspException {

// TODO Auto-generated method stub

//得到uuid

String uuid=UUID.randomUUID().toString().replace("-","");

//得到session,pageContext 是域对象同样也是工具类

pageContext.getSession().setAttribute("token",uuid);

String html="";

try {

pageContext.getOut().print(html);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return EVAL_BODY_INCLUDE;

}

}

3. 编写×.tld文件:

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"

version="2.1">

1.0

apsliyuan

http://my.oschina.net/aps

loginTokenTag

cn.itcast.apsliyuan.tag.LoginTokenTag

JSP

4. jsp中的代码:

pageEncoding="UTF-8"%>

index.jsp

5. 防止乱码的拦截器:

package cn.itcast.apsliyuan.filter;

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class UncodeFilter implements Filter {

public void init(FilterConfig filterConfig) throws ServletException {

// TODO Auto-generated method stub

System.out.println("拦截完成");

}

public void doFilter(ServletRequest req, ServletResponse res,

FilterChain chain) throws IOException, ServletException {

HttpServletRequest request=(HttpServletRequest) req;

HttpServletResponse response=(HttpServletResponse) res;

request.setCharacterEncoding("UTF-8");

response.setContentType("text/html;charset=UTF-8");

chain.doFilter(request, response);

}

public void destroy() {

// TODO Auto-generated method stub

}

}

6. web.xml中的配置:

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">

Tag

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

loginServlet

cn.itcast.apsliyuan.web.servlet.LoginServet

loginServlet

/server/Login

UncodeFilter

cn.itcast.apsliyuan.filter.UncodeFilter

UncodeFilter

/*

希望本文所述对大家的JSP程序设计有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值