您好,
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>
答案 0 :(得分:0)
您想要的是使用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必须路径通过@WebServlet
anotation声明您的案例中的servlet的url-mappling。
此外,如果您不需要get方法 - 请勿使用它。否则,更好的做法是get和post方法都可以调用processRequest(...)
等常用函数。