0

I'm supporting an older JSP system that may be using non-standard way of including classes and functions. Instead of writing classes files and compiling them into jar files, the classes are written directly in "child" JSPs and then included in a "parent" JSP.

The problem I'm having is that in JDeveloper, even though the IDE recognize the files in the include statement, I get error messages that MyClass and ChildFunction() are not defined. When I drop the 2 JSPs on the server, the page loads and runs with the expected results. As a result, when I work on the real code, I have a 1000+ line JSP with over a hundred syntax errors, so I can't tell if my code is even syntactically correct until I drop it on a server. I'm hoping its something simple (/stupid) like a classloader error or a bad configuration, but I haven't been able to fix it. Any help is greatly appreciated.

I've included two stub files below:

MyClass.jsp FILE:

<%@ page import="java.lang.*,java.net.*,java.text.*,javax.servlet.http.*" %>
<%!
class MyClass() {
  public MyClass() { super(); }
  private int it=0;
  public void setIt (int it) {this.it = it; }
  public int getIt () { return it; }
}
public String ChildFunction(){
    return " ChildFunction ";
}
%>

Parent.jsp FILE:

<%@ page contentType="text/html;charset=WINDOWS-1252"%>
<%@ page language="java" session="true" import="java.lang.*,java.net.*,java.text.*,javax.servlet.http.*" %>
<HTML>
<HEAD><TITLE>Test page</TITLE></HEAD>
<BODY>
<%@ include file="./MyClass.jsp" %>
<br>Result of calling Child Function:<% out.print(ChildFunction()); %>
<br>Result of declaring MyClass:
<% 
MyClass mc = new MyClass();
out.print(mc.getIt());
 %>
</BODY>
</HTML>

Before someone suggest that I put the code in class files and use EAR/WAR files to deploy, that's not an option. This is an existing legacy system that I need to be able to support.

dave
  • 11
  • 2

1 Answers1

0

Don't put functions or subclasses in JSPs expecting to use them in others. (That is what they are after all, subclasses under that particular JSP.) It is very bad design. Create classes in actual .java files, compile them, and then call them from the JSPs. Even that is not particularly recommended (see How to avoid Java code in JSP files), but its better, a lot better.

If you have access to the filesystem, you don't need to put classes in a EAR/WAR. Just put them on the filesystem under webapps/{theApp}/WEB-INF/classes/...

You can even dispense with an IDE altogether, and just save all your .java files under webapps/{theApp}/WEB-INF/classes/... open a console, cd to that directory, and javac them. There is certainly no need to put class files in jars.

You do, of course, have to restart the application afterwards.

Community
  • 1
  • 1
developerwjk
  • 8,619
  • 2
  • 17
  • 33