WithdrawServlet
package com.dayuan.servlet;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.dayuan.service.A_CardResver;
import com.dayuan.util.Constant;
@WebServlet("/WithdrawServlet")
public class WithdrawServlet extends HttpServlet {
private static final long serialVersionUID = 1660515624288215260L;
A_CardResver ac = new A_CardResver();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession(true);
Object cardidtemp = session.getAttribute("username");
String method = req.getParameter("method");
if(method.equals("jump")){
//加令牌
String token = UUID.randomUUID().toString();
session.setAttribute("token", token);
req.getRequestDispatcher("WEB-INF/jsp/Withdraw.jsp").forward(req, resp);
return;
}else if (method.equals("main")) {
//验证令牌是否相同
String token1 = req.getParameter("token");
String token2 = String.valueOf(session.getAttribute("token"));
if (!(token1 != null && token1.equals(token2))) {
// 取款成功跳转到取款页面
resp.sendRedirect("InterFaceServlet?message="+URLEncoder.encode("取款不合法","UTF-8"));
return;
}
String withmoney = req.getParameter("withdraw");
String cardid = (String)cardidtemp;
int money = 0;
try {
money = Integer.parseInt(withmoney);
} catch (NumberFormatException e) {
System.out.println("金额不是数字");
}
int result = ac.CheckWithdraw(cardid, money);
//清除令牌
session.removeAttribute("token");
if(result == Constant.BALACE_ERROR) {
resp.sendRedirect("InterFaceServlet?message="+URLEncoder.encode("账户余额不足","UTF-8"));
}else if(result == Constant.LIMIT_ERRPR){
resp.sendRedirect("InterFaceServlet?message="+URLEncoder.encode("今日额度已用完","UTF-8"));
}else if(result == Constant.SUCESS){
resp.sendRedirect("InterFaceServlet?message="+URLEncoder.encode("操作成功","UTF-8"));
}
}
}
@Override
public void destroy() {
}
@Override
public void init() throws ServletException {
}
}
withdraw.jsp
<%@page import="com.dayuan.service.A_CardResver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>取款</title>
</head>
<body>
<form action="WithdrawServlet?method=main" method="post">
输入金额:<input type="number" name="withdraw">
<input name="token" type="hidden" value="${token }"/>
<input type="submit"value="确认">
<a href="InterFaceServlet">返回菜单</a>
</form>
</body>
</html>