0

I am trying to make my first web application with java servlet, apache tomcat and java. This are my directories enter image description here

enter image description here

enter image description here

This is my java file saved as HelloServlet.java

import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
public class HelloServlet extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        out.println("<html>\n\t<head>\n\t\t<title>Hello!</title>\n\t</head>");
        out.println("\t<body>\n\t\tHello, world.\n\t</body>");
    }
}

The java file compiled just fine no problem there.

This is my web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 
2.3//EN" "http://java.sun.com/dtd/Web-app_2_3.dtd">
<web-app>
    <display-name>The Hello World Application</display-name>
    <description>Prints hello world!</description>
    <servlet>
        <servlet-name>hello</servlet-name>
        <description>This is a simple Servlet</description>
        <servlet-class>HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</Web-app>

Apache Tomcat is successfully installed and running just fine I changed the port number to 8888 which was also just fine.

but if I visit http://localhost:8888/hello I get a directory list of the folder hello instead of a simple web page with Hello, world. Why so?

  • 2
    STRONG SUGGESTION: consider developing your Java servlets in an IDE like Eclipse or Netbeans. – paulsm4 May 27 '22 at 00:08
  • I will this is the first one – Robert Mdee May 27 '22 at 00:11
  • 1
    When you visit http://localhost:8888/ do you see a page branded with Apache Tomcat? – Basil Bourque May 27 '22 at 00:28
  • 1
    check your catalina.out log file – Scary Wombat May 27 '22 at 00:30
  • @BasilBourque Yes I do – Robert Mdee May 27 '22 at 00:37
  • @ScaryWombat Do I find it here C:\Program Files\Apache Software Foundation\Tomcat 10.0\logs – Robert Mdee May 27 '22 at 00:37
  • @ScaryWombat I searched catalina.out in tomcat10.0 folder no items matched my search – Robert Mdee May 27 '22 at 00:44
  • @ScaryWombat based on https://stackoverflow.com/a/8239570/16430624 I do have three files catalina.2022-05-24, catalina.2022-05-26 and catalina.2022-05-27 in C:\Program Files\Apache Software Foundation\Tomcat 10.0\logs – Robert Mdee May 27 '22 at 01:39
  • 1
    Well I would check the catalina.2022-05-27 file and search for **hello** (the name of your webapp). Are there some errors? As you *see a page branded with Apache Tomcat* it seems loading the webapp is the likely problem. – Scary Wombat May 27 '22 at 01:54
  • @ScaryWombat Let me see that, but interestingly i have renamed the folder Web-INF to WEB-INF and now I don't see directory list but error HTTP Status 404 – Not Found – Robert Mdee May 27 '22 at 02:29
  • @ScaryWombat Thanks a lot, the error was must be closed by not can't believe I didn't see that. Figured that out in catalina.2022-05-27 file. Thanks again – Robert Mdee May 27 '22 at 02:41
  • I also think naming the folder Web-INF was also a problem. It led to the directory list – Robert Mdee May 27 '22 at 02:43
  • 1
    | can't believe I didn't see that - can not un-see it now – Scary Wombat May 27 '22 at 02:56
  • I suggest you draft, post, and accept an Answer to your own Question, for posterity. – Basil Bourque May 27 '22 at 04:13
  • Using tools to auto-generate this boilerplate would avoid the hassle of your understandable typos. Humans are not good at trivial details, while computers are. For example, choosing *File > New Project > Generators > Java Enterprise* in IntelliJ will create a complete project with a sample JSP page and a sample Servlet. Both the `WEB-INF` folder and its nested `web.xml` file are created automatically for you. – Basil Bourque May 27 '22 at 04:16

0 Answers0