JSP uses custom tags to prevent repeated submission of forms. jsp forms

Source: Internet
Author: User

JSP uses custom tags to prevent repeated submission of forms. jsp forms

This example describes how JSP uses custom tags to prevent repeated submission of forms. Share it with you for your reference. The details are as follows:

1. Write 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. extends; public class LoginServet extends HttpServlet {/*** deserialization */private static final long serialVersionUID = complete; @ Override public void doPost (HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {// obtain the value String token = requset in the input box. getParameter ("tokenName"); // obtain the value String sessionToken = (String) requset in the session. getSession (). getAttribute ("token"); if (token. equals (sessionToken) {response. getWriter (). print ("saved successfully"); requset. getSession (). removeAttribute ("token"); // remove} else {response. getWriter (). print ("sorry, you cannot submit it again ");}}}

2. Compile 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 logtailkentag extends TagSupport {/*** all serializing */private static final long serialVersionUID =-1815168785364991254L; @ Override public int doStartTag () throws JspException {// TODO Auto-generated method stub // obtain uuid String uuid = UUID. randomUUID (). toString (). replace ("-", ""); // get the session. pageContext is a domain object and also 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. Compile the X. tld file:

<? 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"> <! -- Define version --> <tlib-version> 1.0 </tlib-version> <! -- Define the name --> <short-name> apsliyuan </short-name> <! -- Define uri --> <uri> http://my.oschina.net/aps </uri> <tag> <name> logtailkentag </name> <tag-class> cn. itcast. apsliyuan. tag. logtailkentag </tag-class> <body-content> JSP </body-content> </tag> </taglib>

4. Code in 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. Anti-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 ("intercepted");} 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. configuration in 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>

I hope this article will help you with JSP program design.

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.