英文:
How to communicate with a servlet from a Java client
问题
我在 Windows 10 上使用 Netbeans 12 开发了一个 servlet。该 servlet 部署在集成了 Netbeans IDE 的我的计算机上的 Apache Tomcat 9.0.39 上。在开发 servlet 时,Netbeans IDE 会自动创建一个带有静态 HTML 内容的 index.html 文件,并将该文件放在“src->main->webapp”文件夹下。
客户端向 servlet 发送一个字符串,servlet 反转该字符串并将其返回给客户端。然而,我的客户端只在来自 servlet 的响应中看到 index.html 的静态内容(如上所述),而没有看到 servlet 将反转的字符串对象写入 PrintWriter 对象(从 Response.getWriter() 获取)。
下面是 index.html 的内容:
<!DOCTYPE html>
<html>
<head>
<title>My first servlet</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Reversing Strings!!!</h1>
<p> This is index.html page</p>
</body>
</html>
以下是 web.xml 的内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>ReverseString</servlet-name>
<servlet-class>org.me.servletlearning.ReverseString</servlet-class>
<init-param>
<param-name>greeting</param-name>
<param-value>Hello Tony</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ReverseString</servlet-name>
<url-pattern>/reversestring</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
以下是 context.xml 的内容:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/reversestring"/>
以下是 servlet 的内容:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String inputString = request.getParameter("reverse_string");
StringBuilder temp = new StringBuilder(inputString);
String result = temp.reverse().toString();
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet ReverseString</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet ReverseString at " + request.getContextPath() + "</h1>");
out.println("<p> The input string:"+inputString+"<br>");
out.println("The reversed string:"+result+" </p>");
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
以下是客户端的内容:
public static void writeToURL() {
try {
String stringToReverse = URLEncoder.encode("HelloWorld", "UTF-8");
URL url = new URL("http://localhost:8080/reversestring/");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream());
out.write("reverse_string=" + stringToReverse);
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
客户端从 servlet 收到的内容如下:
<!DOCTYPE html>
<html>
<head>
<title>My first servlet</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Reversing Strings!!!</h1>
<p> This is index.html page</p>
</body>
</html>
在我的代码中缺少什么?谢谢。
英文:
I have developed a servlet using Netbeans 12 on Windows 10. The servlet is deployed on Apache Tomcat 9.0.39 on my machine that is integrated with the Netbeans IDE. When developing a servlet, the Netbeans IDE automatically creates an index.html file with static Html content and places the file under the “src->main->webapp” folder.<br>
The client sends a string to the servlet and the servlet reverses the string and returns it back to the client. However my client only sees the static content of the index.html (mentioned above) in the response from the servlet and not the reversed String object that servlet has written to the PrintWriter object (obtained from the Response.getWriter()).
<br>Here is the content of the index.html:
<!DOCTYPE html>
<html>
<head>
<title>My first servlet</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Reversing Strings!!!</h1>
<p> This is index.html page</p>
</body>
</html>
<br>Here is the web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>ReverseString</servlet-name>
<servlet-class>org.me.servletlearning.ReverseString</servlet-class>
<init-param>
<param-name>greeting</param-name>
<param-value>Hello Tony</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ReverseString</servlet-name>
<url-pattern>/reversestring</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
<br>Here is the context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/reversestring"/>
<br>Here is the servlet:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String inputString = request.getParameter("reverse_string");
StringBuilder temp = new StringBuilder(inputString);
String result = temp.reverse().toString();
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet ReverseString</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet ReverseString at " + request.getContextPath() + "</h1>");
out.println("<p> The input string: "+inputString+"<br>");
out.println("The reversed string: "+result+" </p>");
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
<br>The client:
public static void writeToURL() {
try {
// need to study and understand servlet
// then write to a servelt through a URL object.
String stringToReverse = URLEncoder.encode("HelloWorld", "UTF-8");
URL url = new URL("http://localhost:8080/reversestring/");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream());
out.write("reverse_string=" + stringToReverse);
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
<br> The content the client receives from the servlet:
<!DOCTYPE html>
<html>
<head>
<title>My first servlet</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Reversing Strings!!!</h1>
<p> This is index.html page</p>
</body>
</html>
<br>What is missing from my code?
Thanks
答案1
得分: 0
你的 web 应用上下文路径是 /reversestring
。字符串翻转的 servlet 在该上下文中的路径为 /reversestring
。要向 servlet 发送请求,URL 应为:
http://localhost:8080/reversestring/reversestring
英文:
Your webapp context path is /reversestring
. The string-reversing servlet is registered at path /reversestring
within that context. To make a request to the servlet, the URL should be:
http://localhost:8080/reversestring/reversestring
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论