Java使用支付宝接口-demo案例源码架包一起(jsp+servlet)
package zhifubao.servletTest;
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;
import com.alipay.api.AlipayApiException;
import zhifubao.service.order.OrderController;
/**
* @author YXqiang
*/
@WebServlet(description = "支付宝测试支付功能", urlPatterns = { "/mydo" })
public class zhifubaoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
//String outTradeNo, String subject, String totalAmount, String body
System.out.println(req.getParameter("outTradeNo"));//交易编号
System.out.println(req.getParameter("subject"));//商品
System.out.println(req.getParameter("totalAmount"));//总价
System.out.println(req.getParameter("body"));//商品描述信息
String outTradeNo, subject, totalAmount, body;
outTradeNo=req.getParameter("outTradeNo");
subject=req.getParameter("subject");
totalAmount=req.getParameter("totalAmount");
body=req.getParameter("body");
OrderController order=new OrderController();
try {
String formestr =order.alipay(outTradeNo, subject, totalAmount, body);
req.setAttribute("mycalls", formestr);
req.getRequestDispatcher("main.jsp").forward(req, resp);
} catch (AlipayApiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
1