1

I am trying to access static fields presents in my java file from my JSP using EL in Tomcat 8 container but getting error (shown below).

Does Tomcat8 supports EL 3.0? If yes, then what am I missing?

AppConstants.java

package com.study.el;

public final class AppConstants {

    public static final String NAME = "abc";

    private AppConstants() {
    }
}

AppContextListener.java

package com.study.el;

@WebListener
public class AppContextListener implements ServletContextListener {

    private ServletContext appContext;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        JspFactory.getDefaultFactory().getJspApplicationContext(event.getServletContext()).addELContextListener(new ELContextListener() {
            @Override
            public void contextCreated(ELContextEvent event) {
                event.getELContext().getImportHandler().importClass("com.study.el.AppConstants");
            }
        });
    }


    @Override
    public void contextDestroyed(ServletContextEvent contextEvent) {
        //nothing to implement
    }
}

index.jsp

<!DOCTYPE html>
<html>
    <head>
        <title>Start Page</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <h1>Hello World!</h1>
        <h1>Hello ${AppConstants.NAME}</h1>
    </body>
</html>

Error Log:

javax.el.ELException: The class [com.study.el.AppConstants] could not be imported as it could not be found
javax.el.ImportHandler.importClass(ImportHandler.java:114)
Nital
  • 5,784
  • 26
  • 103
  • 195
  • I think the problem is the private constructor. – pL4Gu33 Mar 10 '15 at 19:51
  • I changed the constructor to `public` and re-ran the program and still getting the same exception. – Nital Mar 10 '15 at 19:59
  • Possibly duplicate: http://stackoverflow.com/questions/24768377/access-static-property-or-method-in-jsp-via-el-3-0-jee7-tomcat-8 – Naman Mar 11 '15 at 07:31
  • @NaMaN - Tried replacing the `jsp-api.jar` with `javax.servlet.jsp-api-2.3.2-b01.jar` and renamed it to `jsp-api.jar` and put it in Tomcat8/lib directory. Also changed the JSP by replacing `Hello ${AppConstants.NAME}` with `Testing: ${Boolean.TRUE}` and still won't work – Nital Mar 11 '15 at 22:21

2 Answers2

0

but If you try to change this statement "private ServletContext appContext;" to " public ServletContext appContext;"

0

now you try import your class :

<%@ page import=”your_package.your_class_name” %>
Abraham Uribe
  • 3,118
  • 7
  • 26
  • 34