-1

I have a doubt:

  1. Is it possible to call a Java class with main() in JSP and print the value in console or JSP page (without use of a Servlet class)?

  2. Similarly print the value in JSP page from Java class with main() (without use of a Servlet class)?

please need some explanation.

jmj
  • 237,923
  • 42
  • 401
  • 438
supreeth v
  • 19
  • 8
  • What do you mean by "without use of a Servlet class"? JSP files are translated to servlets so it is hard to not use them. – Pshemo Mar 16 '15 at 20:09
  • @Pshemo : Without use meaning I want to have a call from Jsp and Java(only),just wanted to understand is this concept possible. Or is it a rule that a call from Jsp has to go via servlet and then Java class – supreeth v Mar 16 '15 at 20:18
  • Call from JSP is in reality call from servlet (since JSP is nicer servlet). You can add some code in JSP which will invoke some methods with little help of scriptlets `<% ... %>` but [you should avoid it](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files). About point 2 I am not sure what you want to achieve since running servlets (and JSP) is servers job. – Pshemo Mar 16 '15 at 20:23
  • @Pshemo: Thanks for the explanation and helping to understand the concept – supreeth v Mar 16 '15 at 20:25

2 Answers2

0

Is it possible to call a Java class with main() in JSP and print the value in console or JSP page (without use of a Servlet class)?

Similarly print the value in JSP page from Java class with main() (without use of a Servlet class)?

Any hack is possible, but Servlet, JSP, and JSTL is best suited here

Checkout tutorial

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
0

Since a typical main() method has return type void, this cannot be done:

public staic void main(String[] args) { ... }

But you call any static method on that class and return a String and output that to your JSP:

Class

public class Util {
  public static String doSomething() {
    // do something and generate a String
    return "helloWord";
  }
}

JSP:

<%= Util.doSomething() %>

this prints out the return value of your static doSomething() method at where the JSP output tag is included.

Alexander
  • 2,925
  • 3
  • 33
  • 36