JSP使用自定义标签防止表单重复提交的方法
网络编程 2021-07-05 11:23www.168986.cn编程入门
这篇文章主要介绍了JSP使用自定义标签防止表单重复提交的方法,以实例形式较为详细的分析了JSP基于自定义标签防止表单重复提交的具体步骤与实现方法,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了JSP使用自定义标签防止表单重复提交的方法。分享给大家供大家参考。具体如下
1. 编写servelt
package .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 .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./xml/ns/javaee" xmlns:xsi="http://.w3./2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun./xml/ns/javaee http://java.sun./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./aps</uri> <tag> <name>loginTokenTag</name> <tag-class>.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./aps" prefix="aps" %> <%@taglib uri="http://java.sun./jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://.w3./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 .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://.w3./2001/XMLSchema-instance" xmlns="http://java.sun./xml/ns/javaee" xmlns:web="http://java.sun./xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun./xml/ns/javaee http://java.sun./xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Tag</display-name> <wele-file-list> <wele-file>index.html</wele-file> <wele-file>index.htm</wele-file> <wele-file>index.jsp</wele-file> <wele-file>default.html</wele-file> <wele-file>default.htm</wele-file> <wele-file>default.jsp</wele-file> </wele-file-list> <servlet> <servlet-name>loginServlet</servlet-name> <servlet-class>.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>.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中自定义标签用法实例分析
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程