我想處理一個HTTP請求並對其進行一些處理。我可以做以下嗎?在doPost servlet中調用Java函數?
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/soap+xml;charset=utf-8");
char[] cbuf = new char[10000];
PrintWriter out = response.getWriter();
InputStreamReader in = new InputStreamReader(request.getInputStream());
in.read(cbuf,0,10000);
int req_len = request.getContentLength();
String inputLine = new String(cbuf);
Spoof sp = new Spoof();
String xml_response = sp.spoof_second(inputLine.substring(0, req_len), request.getParameter("mode"));
out.println(xml_response);
}
我得到一個空響應,所以我想知道是否有方法來調用函數spoof_second()。
我的web.xml看起來像:
<?xml version="1.0"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>home</servlet-name>
<servlet-class>website.web.HomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
,我將請求發送到[HTTP://本地主機:4321 /網站/回家模式=記錄]這樣的模式是正確的並且inputLine也是正確的,因爲我通過將它們打印出來進行檢查。我不確定的是如何在Eclipse中構建整個項目,因爲我認爲函數調用沒有進行。我沒有得到任何異常,我得到一個空的內容的HTTP 200 OK響應。
你可以調用doPost中的方法;你還有別的錯誤。我敢打賭,inputLine是空的。 – duffymo
'doPost'是一種方法,而在另一種方法中調用的方法在Java中是完全有效的,所以你的問題應該是別的,也許如果你發佈了異常,我們可以幫助多一點 – higuaro
System.out xml_response。我認爲這是一個空值。 –