Issue
How can I configure a web application so that .jsp files in subdirectories can find the same classes as .jsp files in the application's root directory?
I have moved an old web application from a tomcat6 server to a tomcat9 server. However, .jsp files in subdirectories of the app are no longer able to import java classes that are under WEB-INF/classes. The web app is installed as an exploded war file. To demonstrate the problem I have a test.jsp file that just contains:
<%@ page import="com.example.serverutils.StringUtil" %>
<%= StringUtil.MILLISECONDS_PER_DAY %>
This file compiles fine when it is in the application's root directory. But if I move test.jsp to a subdirectory, it is unable to find the StringUtil class.
The directory structure is as follows:
domain\test.jsp
domain\sub\test.jsp
domain\WEB-INF\web.xml
domain\WEB-INF\classes\com\example\serverutils\StringUtil.class
The actual error being logged is "Only a type can be imported. com.example.serverutils.StringUtil resolves to a package"
Solution
I figured out what was causing the issue by comparing how the app was configured in the server.xml file in the old (tomcat6) and new (tomcat9) conf directory.
The configuration that was NOT working had
<Host appBase="/path/to/root/of/webapp">
<Context docBase="">
But the configuration that was working had
<Host appBase="/path/to/root/of">
<Context docBase="webapp">
Apparently, having the Context's docBase set to "" causes this behavior. Changing the server.xml so that the Context's docBase has the name of the last subdirectory of the path and having the Host element's appBase set to one directory up from there fixed this problem.
Answered By - John Weidner