JSP使用自訂標籤防止表單重複提交的方法_JSP編程

來源:互聯網
上載者:User

本文執行個體講述了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程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.