gpt4 book ai didi

java - 在测试 servlet 时在我的浏览器输出中获取字符串

转载 作者:行者123 更新时间:2023-11-28 22:56:49 24 4
gpt4 key购买 nike

我正在运行以下 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 进行部署。我怀疑,在 Test_Servlet 方法中定义的变量 result 中返回的结果没有在 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]);
}

最佳答案

您的 Test_Servlet 方法返回一个 String 数组。在数组上调用 toString() 的结果是您获得的字符串,其中包括类型和对象 ID。

不确定您到底想做什么,但您可以通过遍历返回的数组来将字符串输出。

关于java - 在测试 servlet 时在我的浏览器输出中获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25047075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com