It is a very difficult question to submit a beginner Form for JSP beginners. How can we avoid multiple submission of Form forms? You can start from the following aspects:
1. For beginner JSP, you can set it in Javascript first.
Set a variable and only allow the form to be submitted once.
- ﹤script language="javascript"﹥
- var checkSubmitFlg = false;
- function checkSubmit() {
- if (checkSubmitFlg == true) {
- return false;
- }
- checkSubmitFlg = true;
- return true;
- }
- document.ondblclick = function docondblclick() {
- window.event.returnValue = false;
- }
- document.onclick = function doconclick() {
- if (checkSubmitFlg) {
- window.event.returnValue = false;
- }
- }
- ﹤/script﹥
-
- ﹤html:form action="myAction.do" method="post" onsubmit="return checkSubmit();"﹥
2. for JSP, you must master another Javascript setting.
Set the form submission button or image to disable
- ﹤html:form action="myAction.do" method="post"
- onsubmit="getElById('submitInput').disabled = true; return true;"﹥
- ﹤html:image styleId="submitInput" src="images/ok_b.gif" border="0" /﹥
- ﹤/html:form﹥
3. Be good at using the STRUTS synchronization token mechanism in the entry stage of JSP.
The synchronous Token mechanism is used to solve the problem of repeated Form submission in Web applications. Struts also provides a reference implementation.
Basic Principles:
Before processing the incoming request, the server compares the token value contained in the request with the token value saved in the current user session to see if it matches. After the request is processed and the reply is sent to the client, a new token will be generated. In addition to sending the token to the client, the old token saved in the user session is also replaced. In this way, if the user goes back to the submission page and submits the request again, the token sent from the client is inconsistent with the token sent from the server, effectively preventing repeated submission.
- If (isTokenValid (request, true )){
- // Your code here
- Return mapping. findForward ("success ");
- } Else {
- SaveToken (request );
- Return mapping. findForward ("submitagain ");
- }
STRUTS generates a unique token for each session based on the user session ID and the current system time. For details, refer to the generateToken () method in the TokenProcessor class.
1. // verify the transaction control token.
2. In action:
- // <InputType="Hidden" Name="Org.apache.struts.taglib.html. TOKEN"
- //Value="6aa35341f25184fd996c4c918255c3ae">
- If (! IsTokenValid (request ))
- Errors. add (ActionErrors. GLOBAL_ERROR,
- New ActionError ("error. transaction. token "));
- ResetToken (request); // Delete the token in the session
3. action has such a method to generate a token
- protected String generateToken(HttpServletRequest request) {
- HttpSession session = request.getSession();
- try {
- byte id[] = session.getId().getBytes();
- byte now[] =
- new Long(System.currentTimeMillis()).toString().getBytes();
- MessageDigest md = MessageDigest.getInstance("MD5");
- md.update(id);
- md.update(now);
- return (toHex(md.digest()));
- } catch (IllegalStateException e) {
- return (null);
- } catch (NoSuchAlgorithmException e) {
- return (null);
- }
- }
During update, the form button is prevented from being repeatedly clicked. The Session is used for judgment.
4. Master the page mode when getting started with JSP
- ﹤input type="hidden " name=" ﹤% =com.lims.util.SynchroToken.TOKEN_NAME%﹥ " value =" ﹤%= com.lims.util.SynchroToken.getToken(request)%﹥" ﹥
-
- SynchroToken.java
- package com.lims.util;
- import org.apache.struts.util.*;
- import javax.servlet.http.*;
- import javax.servlet.jsp.*;
- import org.apache.struts.action.*;
- /**
- * ﹤p﹥Title: SynchroToken ﹤/p﹥
- * ﹤p﹥Description: ﹤/p﹥
- * ﹤p﹥Copyright: Copyright (c) 2004﹤/p﹥
- * ﹤p﹥Company: NetStar﹤/p﹥
- * @author Jstar
- * @version 1.0
- * Created in 2004/04/21
- */
- public class SynchroToken{
- public final static java.lang.String TOKEN_NAME = "_token";
- public static boolean checkToken (HttpServletRequest request){
- boolean isEqual = false;
- HttpSession session = request.getSession ();
- String formToken = request.getParameter (TOKEN_NAME);
- String sessionToken = (String)session.getAttribute (TOKEN_NAME);
- System.out.println ("formToken: " + formToken + " sessionToken: " +
- sessionToken);
- if (formToken != null && sessionToken == null){
- session.setAttribute (TOKEN_NAME, formToken);
- isEqual = true;
- }
- return isEqual;
- }
- /**
- * Insert the method's description here.
- * Creation date: (4/19/2004 3:23:25 PM)
- * @return java.lang.String
- * @param request javax.servlet.http.HttpServletRequest
- */
- public static String getToken (HttpServletRequest request){
- String token = "" + System.currentTimeMillis ();
- HttpSession session = request.getSession ();
- if (session != null){
- session.removeAttribute (TOKEN_NAME);
- }
- return token;z
- }
- /**
- * Insert the method's description here.
- * Creation date: (4/19/2004 3:24:10 PM)
- * @return java.lang.String
- */
- final static java.lang.String getTOKEN_NAME (){
- return TOKEN_NAME;
- }
- public static String message (PageContext pageContext, String key) throws
- JspException{
- return RequestUtils.message (pageContext, null, null, key);
- }
- }
-
5. You can also add a transit page
From the perspective of getting started with JSP, you can add a transit page. Although this is not visually good, the page is obviously cumbersome, however, this is a good way to effectively avoid repeated submission of Form forms.
- Use JSP include mechanism to improve appearance
- Six common issues for JSP Programming
- Use of scripts Based on JSP tutorials
- How Tomcat improves performance on JSP pages
- What is JSP and its strong weakness?