2014-10-20 84 views
0

我需要使用Authorize.net AIM方法在線對信用卡進行收費。作爲一個起點,我創建了一個java servlet來調用Authorize.net的示例JSP代碼。我是一個Java新手,這是我第一次使用JSP。Java servlet調用(authorize.net)示例JSP代碼返回空白頁

該servlet編譯得很好,但是當我在瀏覽器中訪問該servlet時,它將返回一個空白屏幕。我期待看到sample.jsp生成的輸出(儘管它不是真正的事務)。任何人都知道爲什麼它沒有顯示出來?

我猜測它與servlet的使用變量res和簡單地使用out的JSP文件寫入屏幕有關。我不需要在JSP文件中使用類似PrintWriter out = res.getWriter();的東西嗎?或者,變量res如何傳入JSP文件以使JSP文件可以編寫瀏覽器等?我不清楚這一點。有人可以給我一個完整的檢查嗎?

-------- Java Servlet的-------

public class Payment extends HttpServlet { 

    @Override 
    public void init(ServletConfig config) throws ServletException { 
    super.init(config); 
    } 

    protected void doGet(HttpServletRequest req, HttpServletResponse res) 
    throws ServletException, IOException { 

    try { 
     // call sample.jsp 
     req.getRequestDispatcher("/sample.jsp").include(req,res); 

    } catch (Exception e) { 
     ... 
    } finally { 
     ... 
    } 
    } 
} 

--------從Authorize.net sample.jsp;坐在同一個目錄Payment.class ---------

<!-- This sample code is designed to connect to Authorize.net using the AIM method. 
    For API documentation or additional sample code, please visit: http://developer.authorize.net --> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<HTML lang='en'> 
<HEAD> 
    <TITLE> Sample AIM Implementation </TITLE> 
</HEAD> 

<BODY> 
<P> This sample code is designed to generate a post using Authorize.net 
Advanced Integration Method (AIM) and display the results of this post to 
the screen. </P> 
<P> For details on how this is accomplished, please review the readme file, 
the comments in the sample code, and the Authorize.net AIM API documentation 
found at http://developer.authorize.net </P> 
<HR /> 

<%@ page import="java.util.*" %> 
<%@ page import="java.net.*" %> 
<%@ page import="java.io.*" %> 
<%@ page import="javax.net.ssl.*" %> 
<%@ page import="java.net.URLEncoder" %> 
<% 

// By default, this sample code is designed to post to our test server for 
// developer accounts: https://test.authorize.net/gateway/transact.dll 
// for real accounts (even in test mode), please make sure that you are 
// posting to: https://secure.authorize.net/gateway/transact.dll 
URL post_url = new URL("https://test.authorize.net/gateway/transact.dll"); 

Hashtable post_values = new Hashtable(); 

// the API Login ID and Transaction Key must be replaced with valid values 
post_values.put ("x_login", "API_LOGIN_ID"); 
post_values.put ("x_tran_key", "TRANSACTION_KEY"); 

post_values.put ("x_version", "3.1"); 
post_values.put ("x_delim_data", "TRUE"); 
post_values.put ("x_delim_char", "|"); 
post_values.put ("x_relay_response", "FALSE"); 

post_values.put ("x_type", "AUTH_CAPTURE"); 
post_values.put ("x_method", "CC"); 
post_values.put ("x_card_num", "4111111111111111"); 
post_values.put ("x_exp_date", "0115"); 

post_values.put ("x_amount", "19.99"); 
post_values.put ("x_description", "Sample Transaction"); 

post_values.put ("x_first_name", "John"); 
post_values.put ("x_last_name", "Doe"); 
post_values.put ("x_address", "1234 Street"); 
post_values.put ("x_state", "WA"); 
post_values.put ("x_zip", "98004"); 
// Additional fields can be added here as outlined in the AIM integration 
// guide at: http://developer.authorize.net 

// This section takes the input fields and converts them to the proper format 
// for an http post. For example: "x_login=username&x_tran_key=a1B2c3D4" 
StringBuffer post_string = new StringBuffer(); 
Enumeration keys = post_values.keys(); 
while(keys.hasMoreElements()) { 
    String key = URLEncoder.encode(keys.nextElement().toString(),"UTF-8"); 
    String value = URLEncoder.encode(post_values.get(key).toString(),"UTF-8"); 
    post_string.append(key + "=" + value + "&"); 
} 

// The following section provides an example of how to add line item details to 
// the post string. Because line items may consist of multiple values with the 
// same key/name, they cannot be simply added into the above array. 
// 
// This section is commented out by default. 
/* 
String[] line_items = { 
    "item1<|>golf balls<|><|>2<|>18.95<|>Y", 
    "item2<|>golf bag<|>Wilson golf carry bag, red<|>1<|>39.99<|>Y", 
    "item3<|>book<|>Golf for Dummies<|>1<|>21.99<|>Y"}; 

for (int i = 0; i < line_items.length; i++) { 
    String value = line_items[i]; 
    post_string.append("&x_line_item=" + URLEncoder.encode(value)); 
} 
*/ 

// Open a URLConnection to the specified post url 
URLConnection connection = post_url.openConnection(); 
connection.setDoOutput(true); 
connection.setUseCaches(false); 

// this line is not necessarily required but fixes a bug with some servers 
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 

// submit the post_string and close the connection 
DataOutputStream requestObject = new DataOutputStream(connection.getOutputStream()); 
requestObject.write(post_string.toString().getBytes()); 
requestObject.flush(); 
requestObject.close(); 

// process and read the gateway response 
BufferedReader rawResponse = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
String line; 
String responseData = rawResponse.readLine(); 
rawResponse.close();       // no more data 

// split the response into an array 
String [] responses = responseData.split("\\|"); 

// The results are output to the screen in the form of an html numbered list. 
out.println("<OL>"); 
for(Iterator iter=Arrays.asList(responses).iterator(); iter.hasNext();) { 
    out.println("<LI>" + iter.next() + "&nbsp;</LI>"); 
} 
out.println("</OL>"); 
// individual elements of the array could be accessed to read certain response 
// fields. For example, response_array[0] would return the Response Code, 
// response_array[2] would return the Response Reason Code. 
// for a list of response fields, please review the AIM Implementation Guide 
%> 
</BODY> 
</HTML> 

更新1

如果我取代以上sampleJustHTML.jsp sample.jsp,它仍然給一個空白頁。

---------- ----------- sampleJustHTML.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<HTML lang='en'> 
<HEAD> 
    <TITLE> Sample AIM Implementation </TITLE> 
</HEAD> 

<BODY> 
<P> This sample code is designed to generate a post using Authorize.net 
Advanced Integration Method (AIM) and display the results of this post to 
the screen. </P> 

</BODY> 
</HTML> 
+0

您使用調度程序是正確的,但您的jsp位置是錯誤的,據我所知。 如果我是你,首先我將sample.jsp設爲空(只需寫上你好!) 你有一個WEB-INF文件夾嗎?將jsp文件放在WEB-INF文件夾下是可行的。另外編寫req.getRequestDispatcher(「/ WEB-INF/sample.jsp」)。include(req,res); – 2014-10-20 21:50:18

+0

正如我記得如果jsp有錯誤,很難檢測到。 – 2014-10-20 21:52:13

+0

如果你把jsp文件放在WEB-INF文件夾下,你可以使用像:req.getRequestDispatcher(「/ WEB-INF/sample.jsp」)。include(req,res); – 2014-10-20 22:04:25

回答

0

,如果你把JSP下,你可以像使用WEB-INF文件夾中的文件: req.getRequestDispatcher( 「/ WEB-INF/sample.jsp」)包括(REQ,RES)。

現在我將猜測:req.getRequestDispatcher(「/ WEB-INF/classes/sample.jsp」)。include(req,res);

+0

對不起,我的名聲太低,我無法贊成。一旦我將文件路徑改爲以'/ WEB-INF/...'文件夾開始的'jsp'文件,它就起作用了。 – user46688 2014-10-20 22:12:41

+0

不,這不重要。 – 2014-10-20 22:13:37