Issue
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.
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()).
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>
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>
Here is the context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/reversestring"/>
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);
}
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();
}
}
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>
What is missing from my code?
Thanks
Solution
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
Answered By - dnault