0

I have a JSP in some legacy code that declares some functions:

<%!

String func1(String A) {
}

String func2(String B) {
}

%>

Other code includes these JSPs, and just calls func1 and func2 directly.

I want to move those functions to java without modification, so Intellij can operate on them reasonably (show warnings, refactor, etc.) without upgrading Intellij to use JSPs.

I want to move them directly (i.e. cut and paste) without having to rewrite them, because there are thousands of lines of code. I also want the function calls in all the other JSPs to remain valid, so I don't have to rewrite them.

How can I do this?

I see the servlet intro page, which hints that I could replace the JSP with a servlet, but it's not quite clear how. Also, I have hundreds of files that include the existing JSP, so I don't want to delete the page, just have all its functions be defined in a Java.

I also see example questions showing how to declare functions, but not really how to declare some of those functions in Java.

EDIT:

From this answer, I am using

  • Server Version: Apache Tomcat/7.0.108
  • Servlet Version: 3.0
  • JSP Version: 2.2

Yes, it's old. This is a legacy app.

dfrankow
  • 20,191
  • 41
  • 152
  • 214
  • Your Tomcat should be ok for my first answer. See https://stackoverflow.com/questions/11273626/call-methods-in-expression-language – rickz Sep 02 '22 at 00:57

1 Answers1

1

If you are using a recent version of a Servlet container, then you can set an object into application scope and access its methods. Here is some demonstration code. Compile this file and put it your classpath(in your web app's classes folder for example).

package rick;
public class MyFunctions {
    public String func1(String a) {
        return " hello " + a;
    }

    public String func2(String b) {
        return " Hello " + b;
    }
}     

In your JSP, you could use something like

<%@ page import="rick.MyFunctions"%>
<%
    application.setAttribute("my", new MyFunctions());
%>
${my.func1("John")}
${my.func2("Jane")}  

The output is hello John Hello Jane

An alternative solution could be used to access an object and it's methods in a scriptlet. Compile this file and put into your web app's classes folder.

package rick;
public class MyFunctions2 {
    public String func12(String a) {
        return " hello2 " + a;
    }

    public String func22(String b) {
        return " Hello2 " + b;
    }
}    

In your JSP, you could use something like

<jsp:useBean id = "my2" class = "rick.MyFunctions2"></jsp:useBean>  
<%
  out.print(my2.func12("Joe"));
  out.print(my2.func22("Mary"));
%>

Output is hello2 Joe Hello2 Mary

rickz
  • 4,324
  • 2
  • 19
  • 30
  • Thank you! FYI I added server, servlet, and JSP version to the question. Server Version: Apache Tomcat/7.0.108 Servlet Version: 3.0 JSP Version: 2.2. Is that new enough? – dfrankow Sep 02 '22 at 00:45
  • Any way to call the functions directly, without the `${}` notation? There are thousands of lines calling func1 and func2 directly. – dfrankow Sep 02 '22 at 00:46
  • I will give a second answer to access in a scriptlet. – rickz Sep 02 '22 at 00:58
  • Thanks, that looks the closest to what I wanted. It still requires some modification to the calling code (and I want none), but maybe what I want is not possible. – dfrankow Sep 02 '22 at 17:39
  • There's some Java thing that's hooking up the JSP functions in some namespace. I do wonder if I could express that same namespace in Java. Not sure. – dfrankow Sep 02 '22 at 17:40
  • Looking at the generated java code, it looks like it puts the classes in `package org.apache.jsp;` (!). Maybe I could just declare that package and throw things in there too? Pretty hacky :), but it's not for production. – dfrankow Sep 02 '22 at 17:43
  • Please show us an example of the code that you are referring to. – rickz Sep 02 '22 at 23:13