index.html not rendering servlet 3.0 Tomcat 7

560 views Asked by At

I have a simple web app using Servlet 3.0 using Tomcat 7.0.25 and deploying the app as a war (mywebapp.war).

The war structure is as below:

  • mywebapp/index.html
  • mywebapp/META-INF/MANIFEST.MF
  • mywebapp/WEB-INF/classes/org/iq/adapter/ServerAdapter.class

index.html:

<html>
  <head>
    <title>my web app</title>
  </head>
  <body>
    <h1>Your server is Up and Running</h1>
  </body>
</html>

ServerAdapter.java:

package org.iq.adapter;

@WebServlet(name="ServerAdapter", urlPatterns="/adapter/*")
public class ServerAdapter extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        System.out.println(this.getClass()+"::doGet called");
    }

    @Override
    public void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        System.out.println(this.getClass()+"::doPost called");
    }
}

When I try to access mywebapp from browser using the below links:

  • localhost:8080/mywebapp - I get a blank screen, index.html is not rendered - WHY? I guess since welcome file is not mentioned as I am not using a web.xml
  • localhost:8080/mywebapp/index.html - I get a blank screen, index.html is still not rendered - WHY? I am lost
  • localhost:8080/mywebapp/adapter - I get a blank screen, but I get the sysout on the server console as "class org.iq.adapter.ServerAdapter::doGet called" - as expected
1

There are 1 answers

4
kirti On

Create a web.xml file in Web-INF folder like the following and add index.html in welcome files list

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

 <welcome-file-list>  
   <welcome-file>index.html</welcome-file>  
    </welcome-file-list>  

  <servlet>
    <servlet-name>ServerAdapter </servlet-name>
    <servlet-class>org.iq.adapter.ServerAdapter </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>ServerAdapter </servlet-name>
    <url-pattern>/adapter</url-pattern>
</servlet-mapping>
</web-app>