ajax请求后台servlet的方法,url只写servlet类的@WebServlet("/Sign")对应的Sign就好
servlet类:
package demo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Sign")
public class Sign extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
String version=request.getParameter("version");
String tranCode=request.getParameter("tranCode");
String merchantId=request.getParameter("merchantId");
String merOrderNum=request.getParameter("merOrderNum");
String bussId=request.getParameter("bussId");
String tranAmt=request.getParameter("tranAmt");
String sysTraceNum=request.getParameter("sysTraceNum");
String tranDateTime=request.getParameter("tranDateTime");
String currencyType=request.getParameter("currencyType");
String merURL=request.getParameter("merURL");
String backURL=request.getParameter("backURL");
String orderInfo=request.getParameter("orderInfo");
String userId=request.getParameter("userId");
String dataKey=request.getParameter("dataKey");
MD5 md5 = new MD5();
String txnString=version+"|"+tranCode+"|"+merchantId+"|"+merOrderNum+"|"+bussId
+"|"+tranAmt+"|"+sysTraceNum+"|"+tranDateTime+"|"+currencyType+"|"+merURL+"|"+backURL+"|"+orderInfo+"|"+userId;
String signValue = md5.getMD5ofStr(txnString+dataKey);
PrintWriter writer = response.getWriter();
writer.print(signValue);
writer.flush();
writer.close();
}
}
前台html:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.UUID" %>
<%
String uuid = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 14) + "_CS";
String path=request.getContextPath();
%>
<!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>
<script type="text/javascript" src="<%=path%>/js/jquery.js"></script>
</head>
<body onload="show()">
<form method='post' target="" name='tradeform' id='tradeform' action='http://192.168.104.30:7002/NetPay/SynonymNamePay.action'>
version:<input type='text' name='version' id='version' value='1.0.0'/><br/>
transCode:<input type='text' name='transCode' id='transCode' value='8888'/><br/>
merchantId:<input type='text' name='merchantId' id='merchantId' value='888888888888888'/><br/>
merOrderNum:<input type='text' name='merOrderNum' id='merOrderNum' value='<%=uuid%>'/><br/>
bussId:<input type='text' name='bussId' id='bussId' value='ONL0017'/><br/>
tranAmt:<input type='text' name='tranAmt' id='tranAmt' value='20'/><br/>
sysTraceNum:<input type='text' name='sysTraceNum' id="sysTraceNum" value=''/><br/>
tranDateTime:<input type='text' name='tranDateTime' id="tranDateTime" value=''/><br/>
currencyType:<input type='text' name='currencyType' id='currencyType' value='156'/><br/>
merURL:<input type='text' name='merURL' id='merURL' value='http://192.168.101.27:8080/NetPay/test/RcvTxn.jsp'/><br/>
backURL:<input type='text' name='backURL' id='backURL' value="http://192.168.101.27:8080/NetPay/test/RcvTxn.jsp"/><br/>
orderInfo:<input type='text' name='orderInfo' id='orderInfo' value=''/><br/><!-- 如果传需要16进制转换结果 -->
userId:<input type='text' name='userId' id='userId' value=""/><br/>
userNameHF:<input type='text' name='userNameHF' id='userNameHF' value="e794b0e991ab"/><br/><!-- 16进制转换 -->
quickPayCertNo:<input type='text' name='quickPayCertNo' id='quickPayCertNo' value="370112199411012918"/><br/>
arrviedAcctNo:<input type='text' name='arrviedAcctNo' id='arrviedAcctNo' value=""/><br/>
arrviedPhone:<input type='text' name='arrviedPhone' id='arrviedPhone' value="15064010790"/><br/>
arrviedBankName:<input type='text' name='arrviedBankName' id='arrviedBankName' value=""/><br/>
userPhoneHF:<input type='text' name='userPhoneHF' id='userPhoneHF' value="15064010790"/><br/>
userAcctNo:<input type='text' name='userAcctNo' id='userAcctNo' value=""/><br/>
cardCvn2:<input type='text' name='cardCvn2' id='cardCvn2' value=""/><br/>
cardExpire:<input type='text' name='cardExpire' id='cardExpire' value=""/><br/>
userIp:<input type='text' name='userIp' id='userIp' value=""/><br/>
bankId:<input type='text' name='bankId' id='bankId' value="888880170122900"/><br/>
stlmId:<input type='text' name='stlmId' id='stlmId' value=""/><br/>
entryType:<input type='text' name='entryType' id='entryType' value="1"/><br/>
attach:<input type='text' name='attach' id='attach' value=""/><br/>
reserver1:<input type='text' name='reserver1' id='reserver1' value="5"/><br/>
reserver2:<input type='text' name='reserver2' id='reserver2' value=""/><br/>
reserver3:<input type='text' name='reserver3' id='reserver3' value=""/><br/>
reserver4:<input type='text' name='reserver4' id='reserver4' value="7"/><br/>
dataKey:<input type='text' name='dataKey' id='dataKey' value="8EF53C251102A4E6"/><br/>
签名信息:
<textarea rows="10" cols="50" name='signValue' id='signValue'>
</textarea>
<br/>
<input type="button" value="获取签名信息" onclick="mkMD5()"><br/>
<input type="submit" value="提交" ><br/>
</form>
<script type="text/javascript">
function show(){
var date = new Date(); //日期对象
var now = "";
now = date.getFullYear()+''; //读英文就行了
now = now + fill2((date.getMonth()+1)+''); //取月的时候取的是当前月-1如果想取当前月+1就可以了
now = now + fill2(date.getDate()+'');
now = now + fill2(date.getHours()+'');
now = now + fill2(date.getMinutes()+'');
now = now + fill2(date.getSeconds()+'');
document.getElementById("sysTraceNum").value = now; //div的html是now这个字符串
document.getElementById("tranDateTime").value = now; //div的html是now这个字符串
}
function fill2(o){
var length = o.length;
if(length<2){
for(var i=0;i<2-length;i++){
o = '0'+o;
}
}
return o;
}
function mkMD5(){
$.ajax({
type:"post",
url:"Sign",
data:$('#tradeform').serialize(),
success:function(data) {
document.getElementById("signValue").value = data;
},
error:function(data) {
alert("未生成签名");
}
});
}
</script>
</body>
</html>