嗨,
1. 我将这样的表格发送到测试 Sandbox Paypal 服务器
<!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>Start page</title>
</head>
<body>
<form method=post action=https://api-3t.sandbox.paypal.com/nvp>
<input type=hidden name=USER value=my_sandbox_account>
<input type=hidden name=PWD value=my_pass>
<input type=hidden name=SIGNATURE value=my_sign_key>
<input type=hidden name=VERSION value=72.0>
<input type=hidden name=PAYMENTREQUEST_0_PAYMENTACTION
value=Sale>
<input name=PAYMENTREQUEST_0_AMT value=6.00>
<input type=hidden name=RETURNURL
value=http://www.my_return_url.com>
<input type=hidden name=CANCELURL
value=http://www.my_return_url.com>
<input type=submit name=METHOD value=SetExpressCheckout>
</form>
</body>
</html>
2. 提交该表单后,我会在浏览器中收到如下字符串:
TOKEN=EC%23409823094JKK&TIMESTAMP=2013%2d04%2d25T10%3a30%3a54Z&CORRELATIONID=345803985njkk3&ACK=Success&VERSION=72%2e0&BUILD=5709304
3. 我希望我的 servlet 在我的变量中获取这个返回的字符串,以便我可以进一步管理它
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SetExpressCheckoutServlet
*/
@WebServlet("/SetExpressCheckoutServlet")
public class SetExpressCheckoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SetExpressCheckoutServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String token = request.getQueryString();
System.out.println(token.toString());
}
}
当我启动项目时,它只通过了第 1 步和第 2 步,但没有通过第 3 步。
请帮助我正确创建 servlet。
更新
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String token = request.getParameter("TOKEN");
PrintWriter pw = response.getWriter();
if(token == null){
System.out.println("It is null");
} else {
pw.print("<html><body>" + token + "<body></html>");
}
}
我的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">
<display-name>Ppconnector</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SetExpressCheckoutServlet</servlet-name>
<servlet-class>ua.pp.connector.SetExpressCheckoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SetExpressCheckoutServlet</servlet-name>
<url-pattern>/SetExpressCheckoutServlet</url-pattern>
</servlet-mapping>
</web-app>
最佳答案
您想要的是通过已知参数名称获取参数值,使用 ServletRequest#getParameter(String name)
.参数是 get 请求中的那些 name=value
对,它们可以通过调用 getParameter
方法在 servlet 中检索。所以,在你的情况下,它是:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String token = request.getParameter("TOKEN");
//retrieve other parameters in a same way
//and handle parameters accordingly
}
对于要调用的 servlet,您的请求 URL 必须路径 servlet 的 url 映射,在您的情况下,该 servlet 是通过 @WebServlet
注释声明的。
此外,如果您不需要 get 方法,请不要使用它。否则,更好的做法是让 get 和 post 方法都调用一个通用函数,如 processRequest(...)
。
关于java - 使用servlet获取网站返回的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16213717/