1

I have a Tomcat Server and I'm trying to access the index.html file located in the WEB-INF folder, as shown in the picture below

Project directory structure

As seen in the picture, when I open http://localhost:9999/index.html it throws a 404

This is my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="CERTHMaTHiSiSOpenAPI" version="3.1">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>OpenAPI</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>
      api.ws;api.ws.oper
      </param-value>
    </init-param>
    <init-param>
    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
    <param-value>api.ws.oper.Authorization</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>OpenAPI</servlet-name>
    <url-pattern>/api/*</url-pattern>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

Is it a problem that it's located under WEB-INF/lib? I tried moving it to WEB-INF with no success.

Edit: This is my Properties > Web Project Settings Tab: Web Project Properties

It should be noted that this project has also an API, and under Java Resources > src > ws it has a Conductor.java file with this content:

package ws;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;

import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.jaxrs.listing.SwaggerSerializers;
import io.swagger.models.Swagger;
import io.swagger.util.Json;
import ws.util.Config;


@ApplicationPath("/")
public class Conductor extends Application {

@Context ServletContext context;

    public static Properties properties = new Properties();

    public Conductor() {
        super();

        BeanConfig beanConfig = new BeanConfig();
        BeanConfig beanConfig2 = new BeanConfig();

        beanConfig.setVersion("3.1.1");
        beanConfig.setSchemes(new String[]{"https","http"});
//        beanConfig.setHost("localhost:8080");
        beanConfig.setBasePath("/api");
        beanConfig.setResourcePackage("ws.lg");
        beanConfig.setTitle("LG lib Open API");
        beanConfig.setScan(true);

        Swagger swaglg = new Swagger();
        swaglg = beanConfig.getSwagger();
        swaglg.setBasePath("/api/lg");

        beanConfig2.setVersion("3.1.1");
        beanConfig2.setSchemes(new String[]{"https","http"});
//        beanConfig.setHost("localhost:8080");
        beanConfig2.setBasePath("/api");
        beanConfig2.setResourcePackage("ws.sla");
        beanConfig2.setTitle("SLA lib Open API");
        beanConfig2.setScan(true);

        Swagger swaglg2 = new Swagger();
        swaglg2 = beanConfig2.getSwagger();
        swaglg2.setBasePath("/api/sla");

        createSwaggerJsonFile(beanConfig, "swagger-lg.json");
        createSwaggerJsonFile(beanConfig2, "swagger-sla.json");
    }

//  public static final int REST_PORT = 8080;
    @Override
    public Set<Class<?>> getClasses(){
        readProperties();

        Set<Class<?>> resources = new HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }

    private void addRestResourceClasses(Set<Class<?>> resources){
        resources.add(ws.sla.SLAlibOpenAPI.class);
        resources.add(ws.lg.LGlibOpenAPI.class);
        resources.add(ws.auth.Authorization.class);

        resources.add(ApiListingResource.class);
        resources.add(SwaggerSerializers.class);


        //to turn off
//      resources.add(ws.helpers.MongoModifiersAPI.class);
    }


    private Properties readProperties() {
        String fullPath = context.getRealPath(Config.PROPERTIES_FILE);
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File(fullPath));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        if (inputStream != null) {
            try {
                properties.load(inputStream);
            } catch (IOException e) {
                // TODO Add your custom fail-over code here
                e.printStackTrace();
            }
        }else{
            System.err.println("Cannot read config file or no config file exists.");
        }
        return properties;
    }

//  /*
    private static void createSwaggerJsonFile(BeanConfig beanConfig, String filename) {
        try (FileWriter fileWriter = new FileWriter(filename)) {
            File f = new File(filename);
            fileWriter.write(Json.pretty(beanConfig.getSwagger()));
            fileWriter.flush();
            System.out.println("File " + filename + " successfully created in " + f.getAbsolutePath());
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
//    */

}
Tasos
  • 1,575
  • 5
  • 18
  • 44

4 Answers4

2

You should place your html content inside WebContent folder, not inside web-inf. As it searches the index.html in WebContent and its not there so its showing 404 not found error.

Have a look at this question having similar situation and possible answer StackOverFlow Question

Dev
  • 378
  • 2
  • 16
2

The solution was to move all the swagger-ui files to a new folder under WebContent (which I called ui), to move the web.xml to WEB-INF and to add a simple servlet-mapping inside web.xml:

<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>/ui/*</url-pattern>
</servlet-mapping>

the problem was that the /api/* url-pattern in the OpenAPI servlet, was consumed by the API.

Tasos
  • 1,575
  • 5
  • 18
  • 44
  • 1
    To my knownledge web.xml is ONLY to set under WEB-INF. And i think this was your main issue. – slawek Jan 03 '18 at 17:12
  • As I mentioned in the question, I tried moving it to WEB-INF with no success, but it was definitely one of the issues. – Tasos Jan 03 '18 at 23:44
1

I had a similar issue. I was confused between html front pages stored in WebContent, and servlets produced by the Java code.

Tomcat searches for .html files in WebContent, as indicated in Context Parameters. The url-parameter of the servlet mapping tells Tomcat where to look for the servlet itself. To me, it's then logical that both locations needs to be different.

My servlet-mapping looks like <url-pattern>/api/*</url-pattern>. My application class contains @Path('/sname'). While keeping WEB-INF/web.xmlas simple as possible, this gives me access to html pages at localhost:8080/ and to servlets at localhost:8080/api/sname.

Emma
  • 27,428
  • 11
  • 44
  • 69
Ppd47
  • 11
  • 1
0

By default, ROOT war index.html(or welcome-file-list) will be called when you try to access using http://localhost:9999/index.html

If you want to change tomcat default loading page then edit server.xml file and update the following

<Context path="" docBase="new_default_app" />
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
  • My server.xml file had this line included ``. I deleted it and added yours, but when I tried to start the server it throwed errors. It should also be noted that in `Project Properties > Web Project Settings`, the `Context Root` is `api` – Tasos Jan 03 '18 at 08:54
  • did you checked your server port number? – Sasikumar Murugesan Jan 03 '18 at 09:00
  • Inside server.xml, there is this line: `` – Tasos Jan 03 '18 at 09:01
  • Please post stacktrace of your tomcat server error when accessing url it would be easy to find out root cause – Sasikumar Murugesan Jan 03 '18 at 09:07
  • I tried http://localhost:9999/api/index.html, didn't work. This is the error log when I deleted the line I mentioned in the first comment of this answer and added your line in `server.xml`: https://pastebin.com/5WRx2UX8 – Tasos Jan 03 '18 at 09:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162404/discussion-between-crone-and-sasikumar-murugesan). – Tasos Jan 03 '18 at 09:10