欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-08-10 18:45:09
本文实例讲述了jsp使用自定义标签防止表单重复提交的方法。分享给大家供大家参考。具体如下: 1. 编写servelt: package cn.itcast.ap...

本文实例讲述了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="<input type='text' name='tokenname' readonly='readonly' value="+uuid+">";
    try {
      pagecontext.getout().print(html);
    } catch (ioexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
    }
    return eval_body_include;
  }
}

3. 编写×.tld文件:

<?xml version="1.0" encoding="utf-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
  version="2.1">
  <!-- 定义版本 -->
  <tlib-version>1.0</tlib-version>
  <!-- 定义名字 -->
  <short-name>apsliyuan</short-name>
  <!-- 定义uri -->
  <uri>http://my.oschina.net/aps</uri>
  <tag>
    <name>logintokentag</name>
    <tag-class>cn.itcast.apsliyuan.tag.logintokentag</tag-class>
    <body-content>jsp</body-content>
  </tag>
</taglib>

4. jsp中的代码:

<%@ page language="java" contenttype="text/html; charset=utf-8"
  pageencoding="utf-8"%>
  <%@taglib uri="http://my.oschina.net/aps" prefix="aps" %>
  <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>index.jsp</title>
</head>
<body>
  <form action="${pagecontext.request.contextpath}/server/login" method="post">
    <aps:logintokentag/>
    <input type="submit" value="login"/>
  </form>
</body>
</html>

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中的配置:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  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">
  <display-name>tag</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>loginservlet</servlet-name>
    <servlet-class>cn.itcast.apsliyuan.web.servlet.loginservet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>loginservlet</servlet-name>
    <url-pattern>/server/login</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>uncodefilter</filter-name>
    <filter-class>cn.itcast.apsliyuan.filter.uncodefilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>uncodefilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

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