我正在运行以下servlet,我在浏览器上得到一些奇怪的输出。程序工作正常,没有任何错误但由于某种原因,行out.println(Add_To_Queue("abc","xyz","pqr"));
的输出显示
以java字符串的形式,如下所示代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
out.println("<!DOCTYPE> html"); // HTML 5
out.println("<html><head>");
out.println("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
out.println(Test_Servlet("abc","xyz","pqr"));
out.println("<head><title>TEST SERVLET API Call</title></head>");
out.println("<body>");
out.println("<h3>TEST SERVLET</h3>");
// Tabulate the request information
out.println("</body></html>");
}
finally {
out.close(); // Always close the output writer
}
}
public static Object Test_Servlet(String FirstString,String Secondstring,String ThirdString) throws IOException {
String accessKey = "myaccesskey";
String secretKey = "mysecretkey";
String uRLCppList = "http://myurl.com";
String method = "POST";
java.util.Date currentTime = new java.util.Date();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
// Give it to me in GMT time.
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String dateTimeString = sdf.format(currentTime);
String signature = generateSignature(method, secretKey, dateTimeString);
String authorization = accessKey + ":" + signature;
Map<String, String> params = new HashMap<String, String>();
params.put("Content of first String", FirstString);
params.put("Content of Second String", Secondstring);
params.put("Content of Third String", ThirdString);
String[] result = sendHttpRequest(uRLCppList, "POST", params, dateTimeString, authorization);
return result;
}
以下是浏览器输出:
html [Ljava.lang.String;@430bc84a
TEST SERVLET
我正在使用JDK 8,ApacheTomcat 6,Netbeans 7.4进行部署。我怀疑,在result
方法中定义的变量Test_Servlet
中返回的结果无法在Web浏览器上正确显示。
SendHttpRequest方法的附加代码:
public static String[] sendHttpRequest(String requestUrl, String method, Map<String, String> params, String dateTimeString, String authorization) throws IOException {
List<String> response = new ArrayList<String>();
StringBuffer requestParams = new StringBuffer();
if (params != null && params.size() > 0) {
Iterator<String> paramIterator = params.keySet().iterator();
while (paramIterator.hasNext()) {
String key = paramIterator.next();
String value = params.get(key);
requestParams.append(URLEncoder.encode(key, "UTF-8"));
requestParams.append("=").append(URLEncoder.encode(value, "UTF-8"));
requestParams.append("&");
}
}
URL url = new URL(requestUrl);
URLConnection urlConn = url.openConnection();
urlConn.setRequestProperty("accept", "application/json");
urlConn.setRequestProperty("datetime", dateTimeString);
urlConn.setRequestProperty("authorization", authorization);
urlConn.setUseCaches(false);
// the request will return a response
urlConn.setDoInput(true);
if ("POST".equals(method)) {
// set request method to POST
urlConn.setDoOutput(true);
} else {
// set request method to GET
urlConn.setDoOutput(false);
}
if ("POST".equals(method) && params != null && params.size() > 0) {
OutputStreamWriter writer = new OutputStreamWriter(urlConn.getOutputStream());
writer.write(requestParams.toString());
writer.flush();
}
// reads response, store line by line in an array of Strings
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
response.add(line);
}
reader.close();
return (String[]) response.toArray(new String[0]);
}
答案 0 :(得分:1)
您的Test_Servlet
方法返回的数组String
。在数组上调用toString()
的结果是你得到的字符串,其中包括类型和对象Id。
不确定你要做什么,但你可以通过遍历返回的数组来放出字符串。
答案 1 :(得分:0)
html [Ljava.lang.String;@430bc84a
html
来自您需要关闭代码的out.println("<!DOCTYPE> html");
。
[Ljava.lang.String;@430bc84a
是字符串数组的toString的默认值,告诉您引用的存在位置。 [ - 表示数组以及后面的类型恰好是java.lang.String。这是JVM级别输出
我做的事情如下:
String resultStr = "";
for(String s: result){
//format however you wish, this separates each element by a space
resultStr += s + " ";
}
return resultStr
如果要将结果放在表或其他内容中,可以返回数组并使用相同的for循环来封装标记中的每个元素。选择是你的,并且工作正常