网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/11/26 浏览:3 次 )

本文实例讲述了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文件:

<"1.0" encoding="UTF-8" "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中的配置:

<"1.0" encoding="UTF-8""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程序设计有所帮助。

上一篇:JSP学习笔记之基础语法
下一篇:jsp中自定义标签用法实例分析
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap