Learn how Java takes tokens to avoid repeating commits

Source: Internet
Author: User
Tags button type

Duplicate Commit Reason

The jump from the submission page to the Success page is generally based on the view positioning, because the view positioning is on the server to jump, if the user after clicking on the page to refresh again, will result in repeated submissions, the database data will be duplicated.

Take token action 1, generate a random token number on the Transfer Display page, and then put it into session and argument.
 @RequestMapping("/toTrans")//    public String toTrans(ModelMap modelMap, HttpSession session    ) {//!        //        //如果是转账,则先查询余额        String cardNo = (String) session.getAttribute("cardNo");        String balance = cardInfoService.findByCardNo(cardNo).getBalance();        modelMap.addAttribute("balance", balance);        String token = UUID.randomUUID().toString();        session.setAttribute("token",token);        modelMap.addAttribute("token",token);        //跳转到转账页面        return "trans";    }
2, jump to transfer the trans.jsp file, note that the parameter to enter the name and value, otherwise the controller layer can not be found.
```<form id="transForm" class="am-form am-form-horizontal" action="/trans/doTrans.do" method="post"><input name="bToken" id="bToken" value="${token}"><%--传参要用name、value,否则Controller找不到--%><div class="am-form-group">    <div class="am-u-sm-9 am-u-sm-push-3">        <button type="button" onclick="submitForm()" class="am-btn am-btn-primary">提交</button>    </div></div>
 3、获取传参的令牌与session中的令牌比较,看是否一样,一样则进入转账成功页面,否则转账失败。注意进入转账成功页面后需要销毁令牌,以防重复提交。
@RequestMapping("/doTrans")//?public String doTrans(ModelMap modelMap, @RequestParam String bToken, HttpSession session, @RequestParam String checkInCardNo, @RequestParam String realName, @RequestParam String money) {//!    try {        String cardNo = (String) session.getAttribute("cardNo");        String token = (String) session.getAttribute("token");        cardInfoService.forward(cardNo, checkInCardNo, money, realName);        if (!bToken.equals(token) || bToken == null || token == null) {            session.removeAttribute("token");/*此处可删可不删*/            return "fail";        }        session.removeAttribute("token");        return "success";//?为什么success.jsp放在web-inf下面,返回地址会是tans/web-inf...    } catch (Exception e) {        e.printStackTrace();        modelMap.addAttribute("msg", e.getMessage());        return "fail";    }}

```

After the above steps, when the transfer Success page, because the token has been destroyed, when the user refreshed again, will jump to the transfer failure page, thus avoiding the problem of rereading the submission.

Learn how Java takes tokens to avoid repeating commits

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.