how to reference a java .class file from a JSP page?

前端 未结 5 2053
再見小時候
再見小時候 2020-12-19 23:12

I have the JSP file (/page.jsp) in the root of my app directory. I want to use this class located in /WEB-INF/classes/Helper.class.

I tried using the JSP page impo

5条回答
  •  无人及你
    2020-12-19 23:28

    If your class is located directly in /WEB-INF/classes that means it uses default package which is generally not recommended. You don't need to import it because of that; you can use it directly in your JSP:

    <%
     Helper helper = new Helper(); // use appropriate constructor
     %>
    

    A better solution would be to make it a part of package. You'd need to put it into appropriate subfolder of /WEB-INF/classes then, say /WEB-INF/classes/com/mypackage/Helper.class. You'll use fully qualified name or import it in your JSP:

    <%
     com.mypackage.Helper helper = new com.mypackage.Helper(); // use appropriate constructor
     %>
    

提交回复
热议问题