JSP uses a custom label to prevent forms from repeating submissions _jsp programming

Source: Internet
Author: User
Tags stub uuid

The example in this article describes how JSP uses custom labels to prevent forms from repeating submissions. Share to everyone for your reference. Specifically as follows:

1. Preparation of 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 {/** * deserialization/private static final long Serialversionuid = 496070
  7156159691747L; @Override public void DoPost (HttpServletRequest requset, httpservletresponse response) throws Servletexception, IO
    Exception {//Get the value in the input box String token = requset.getparameter ("Tokenname");
    Gets the value in the session string Sessiontoken = (string) requset.getsession (). getattribute ("token");
      if (Token.equals (Sessiontoken)) {Response.getwriter (). Print ("saved successfully"); Requset.getsession (). RemoveAttribute ("token");//To ensure that the values in the session do not repeat, remove} else {Response.getwriter (). Print ("I'm sorry.
    can be repeated "); }
  }
}

2. Write a custom Label class:

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{
  /**
   * where serialization/
  private static final long Serialversionuid = -1815168785364991254l;
  @Override public
  int doStartTag () throws Jspexception {
    //TODO auto-generated a stub
    //Get UUID
    String Uuid=uuid.randomuuid (). toString (). Replace ("-", "");
    Getting Session,pagecontext is a domain object as well as a tool class
    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. Preparation of X.TLD documents:

<?xml version= "1.0" encoding= "UTF-8"?> <taglib xmlns= "Http://java.sun.com/xml/ns/javaee"
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 ">
  <!--defined version-->
  < tlib-version>1.0</tlib-version>
  <!--definition name-->
  <short-name>apsliyuan</short-name >
  <!--definition 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. The code in the 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" >
 
 

5. Prevent garbled Interceptor:

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 ("Intercept complete"); public void Dofilter (ServletRequest req, servletresponse Res, Filterchain chain) throws IOException, Servletexce
      ption {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}}

 

Configuration in

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>i Ndex.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp& Lt;/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> &L T;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>

I hope this article will help you with your JSP programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.