How to call Java class in Jsp

前端 未结 3 1670
清歌不尽
清歌不尽 2021-01-04 13:45

Hi i am trying to call regular java class in jsp page and want to print some on jsp page when i am trying to do i am not getting any output

Here is my code

3条回答
  •  轮回少年
    2021-01-04 14:29

    The JSP useBean declaration is not needed in your code.

    Just use

    
    <%
      MyClass tc = new MyClass();
      tc.testMethod();
    %>
    
    

    But that WILL NOT print anything on the JSP. It will just print Hello on the server's console. To print Hello on the JSP, you have to return a String from your helper java class MyClass and then use the JSP output stream to display it.

    Something like this:

    In java Class

    public String testMethod(){
        return "Hello";
    }
    

    And then in JSP

    out.print(tc.testMethod());
    

提交回复
热议问题