-1

i have a java application which have some open APIs. i want to use those APIs from jsp page. i don't have any idea about this.As of now my understanding is i have to get the instance of running java application.

please correct me if i am wrong.

Ashish
  • 1,527
  • 4
  • 17
  • 33

3 Answers3

2

You have to import the class at the top of the JSP

<%@ page import="my.class.path.MyClass" %>

see How do you import classes in JSP? for details.

Then you can use that class in your Code like this:

<% MyClass myClassInstance = new MyClass();
   myClassInstance.myMethod();
%>

If you want to output something you can use:

<%= myClassInstance.aMethodThatReturnsAString() %>

The method can return anything (but must return something). If its not a String then it will be handled like any other non-String Value in this expression:

"Begin " + aMethodThatReturnsSomething() + " End";

So, an int is displayed like an int, an Object gets its toString() called etc.

Community
  • 1
  • 1
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
1
  • I am assuming that your java application is running on the same server where your jsp is running.
  • Your java application is available as runnable jar.

If above two assumptions are correct then you can call API of that java application by putting it in class path of your application server running jsp and then including it in your jsp.

Now assuming that you want to get some in memory data from your running java application.

  • For this if your java application is listening to some port, and API support is there, your jsp can communicate with that application over the port using the API's
  • In this case, even the jsp and java application can be on different servers too.

The bottom line is , if you can provide some more specific use case details, which java application, what kind of API etc people can help you better

abhinav
  • 1,252
  • 10
  • 27
0

To get the instance of java application through jsp

use following simple steps:

import java clas as
  <%@ page import="classPath.className" %>

Create Object of class and use its method and variable as

<% 
Mycalss test = new Myclass();
test.sum();
%>
Lokesh Kumar
  • 420
  • 3
  • 12