3

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"

John Weidner
  • 2,055
  • 3
  • 18
  • 31
  • I've noticed that with my current configuration that it appears the domain\sub directory is being treated as a separate web application. That is, I can put a second web.xml file at domain\sub\WEB-INF\web.xml. But I really want both the "domain" folder and the "domain\sub" folder to be part of the same web application. – John Weidner Dec 15 '18 at 06:35

2 Answers2

1

Your question may be similar to How to include JSPs file from another folder

It may work with this structure:

domain\test.jsp
domain\import_class.jsp
domain\sub\test.jsp
domain\WEB-INF\web.xml
domain\WEB-INF\classes\com\example\serverutils\StringUtil.class

With domain\import_class.jsp containing:

<%@ page import="com.example.serverutils.StringUtil" %>

And domain\sub\test.jsp containing a relative path include:

<jsp:include page="../import_class.jsp"></jsp:include>
<%= StringUtil.MILLISECONDS_PER_DAY %>
A. STEFANI
  • 6,707
  • 1
  • 23
  • 48
0

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.

John Weidner
  • 2,055
  • 3
  • 18
  • 31