0

I made a website using java servlets and JSP as well as HTML, CSS, and Javascript for the frontend, but for some reason, suddenly, all files except JSP and servlets are being ignored.

Basically, all my CSS, HTML, and Javascript files are being completely ignored so I just get what is on the JSP. I have literally no idea why. For styles, I am forced to use <%@ page contentType="text/css;charset=UTF-8" language="java" %>. If I try to use a CSS file, I get css ignored due to mime type mismatch from my browser.

In addition, the url must end with JSP or be a file mapping for a servlet to actually result in anything. If I visit mywebsite.com/page.html, it doesn't take me to the page even though the html file exists. It only works for JSP or servlet file mapping.

Also, my 404 page only works for JSPs, but not servlets or any other file extension. mywebsite.com/page-that-doesnt-exist or mywebsite.com/page-that-doesnt-exist.html, takes me to my homepage instead of my 404 page. However, if I visit mywebsite.com/page-that-doesnt-exist.jsp I do get redirected to my 404 page.

Just in case you thought this problem couldn't get any weirder, I found that CSS/HTML/any non-JSP files are not ignored if they are outside of the webapps folder, but they are ignored when they are in it.

Things I have Tried

  • Added <mime-mapping><extension>css</extension><mime-type>text/css</mime-type></mime-mapping> to my web.xml.

  • Changing my welcome page's file mapping to something other than "/"

  • Going back to the default error page

My web.xml


<web-app 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_4_0.xsd"
         version="4.0">


    <welcome-file-list>
        <welcome-file>homepage</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>DBusername</param-name>
        <param-value>root</param-value>
    </context-param>

    <context-param>
        <param-name>DBpassword</param-name>
        <param-value>chirag12</param-value>
    </context-param>

    <context-param>
        <param-name>uploadFilePath</param-name>
        <param-value>/usr/share/websitestuff</param-value>
    </context-param>

    ...
    a ton of servlet mappings that I edited out
    ...

    <servlet>
        <servlet-name>CommentCreator</servlet-name>
        <servlet-class>CommentUploader</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CommentCreator</servlet-name>
        <url-pattern>/new-comment</url-pattern>
    </servlet-mapping>

    <error-page>
        <error-code>404</error-code>
        <location>/404</location>
    </error-page>

    <servlet>
        <servlet-name>testservlet</servlet-name>
        <servlet-class>TestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>testservlet</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>

</web-app> 

Any help would be greatly appreciated because I don't understand this problem at all.

bluesquare
  • 76
  • 1
  • 8
  • What build tool are you using? If you're using maven, post the pom.xml file. – Bill Horvath Feb 28 '20 at 05:37
  • Please add more details - version of tomcat, how you are deploying it, etc – John Eipe Feb 28 '20 at 17:53
  • I'm not using any build tools and manually loading the libraries (I tried to use maven but it keeps telling me that it can't find the dependencies despite multiple reinstallations). Version of Tomcat: Tomcat 8.5 How I am depoying it: .war file in the webapps folder of my tomcat installation Sorry, I am not sure what information to provide because I have no idea where this problem is coming from. Just let me know and I will upload it. – bluesquare Mar 01 '20 at 16:16

1 Answers1

0

Your web.xml seems quite short with a lot of mime mapping left out. Here's mine:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <description>OSP Web App.</description>
    <display-name>OSP-WEB</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/configs/appServlet/config-context.xml,
        /WEB-INF/configs/appServlet/aop-context.xml,
        /WEB-INF/configs/appServlet/services-context.xml,
        /WEB-INF/configs/appServlet/security-context.xml,
        /WEB-INF/configs/root-context.xml
        classpath:net/bull/javamelody/monitoring-spring-datasource.xml 
        </param-value>
    </context-param>
    <context-param>
        <param-name>defaultHtmlEscape</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>spring.profiles.active</param-name>
        <!-- <param-value>pro</param-value>-->
        <param-value>dev</param-value>
        <!-- WARNING: DON'T EVER COMMIT THIS FILE TO SUBVERSION -->
        <!-- param "spring.profiles.active" MUST BE "pro" value at SUBVERSION -->
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- <listener> -->
    <!-- <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class> -->
    <!-- </listener> -->
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/configs/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/spring/*</url-pattern>
    </servlet-mapping>
    <distributable />
    <!-- <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
        </filter> -->
    <!-- <filter> -->
    <!-- <filter-name>CharacterEncodingFilter</filter-name> -->
    <!-- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> -->
    <!-- <init-param> -->
    <!-- <param-name>encoding</param-name> -->
    <!-- <param-value>UTF-8</param-value> -->
    <!-- </init-param> -->
    <!-- <init-param> -->
    <!-- <param-name>forceEncoding</param-name> -->
    <!-- <param-value>true</param-value> -->
    <!-- </init-param> -->
    <!-- </filter> -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter>
        <filter-name>HttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        <async-supported>true</async-supported>
    </filter>
    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
        <async-supported>true</async-supported>
    </filter>

    <filter>
        <filter-name>monitoringAccess</filter-name>
        <filter-class>com.thetaedge.onespeks.security.filters.MonitoringFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>username</param-name>
            <param-value>OneSpeksMonitoring</param-value>
        </init-param>
        <init-param>
            <param-name>password</param-name>
            <param-value>M0nit0ring</param-value>
        </init-param>
    </filter>
    <filter>
        <filter-name>monitoring</filter-name>
        <filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
        <async-supported>true</async-supported>
    </filter>
    <!-- <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> 
        <url-pattern>/*</url-pattern> </filter-mapping> -->
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>HttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>monitoringAccess</filter-name>
        <url-pattern>/monitoring</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <async-supported>true</async-supported>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
    <mime-mapping>
        <extension>doc</extension>
        <mime-type>application/vnd.ms-word</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>gif</extension>
        <mime-type>image/gif</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>htm</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>html</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>jpeg</extension>
        <mime-type>image/jpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>jpg</extension>
        <mime-type>image/jpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>js</extension>
        <mime-type>text/javascript</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>pdf</extension>
        <mime-type>application/pdf</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>png</extension>
        <mime-type>image/png</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>txt</extension>
        <mime-type>text/plain</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>xls</extension>
        <mime-type>application/vnd.ms-excel</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>xml</extension>
        <mime-type>text/xml</mime-type>
    </mime-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>
</web-app>

Maybe you could add the required and missing ones.

Ezani
  • 535
  • 3
  • 18
  • I remember the static files working out of the box without listing them here. But I haven't used tomcat for a while. – John Eipe Feb 28 '20 at 17:52
  • I tried adding mime mapping for my CSS, and it didn't work. In addition, everything was working before adding mime-mapping, but suddenly stopped working. – bluesquare Mar 01 '20 at 16:15
  • Don't add the mimes above to your CSS files. Add it to your web.xml file. – Ezani Mar 02 '20 at 03:08