Get attribute from ServletContext on JSP page

How can I find my attribute from a ServletContext object in a JSP page?

I installed it before:

public class MyServletContextListener implements ServletContextListener{ private static final Logger logger = LoggerFactory.getLogger(MyServletContextListener.class); @Override public void contextInitialized(ServletContextEvent event) { logger.info("Init gameEngine in listener"); Engine engine = Engine.getInstance(); event.getServletContext().setAttribute("engine", engine); } @Override public void contextDestroyed(ServletContextEvent event) { }} 

and now you want to go to the JSP page. Maybe this can be done with ${pageContext.servletContext.attributeNames} ?

+6
source share
1 answer

using jstl you can directly get the application object in jsp

 ${applicationScope['attributeNames']} 

using this expression, you can get your application level object directly in jsp

OR

using scriptlet can also get the application object in jsp and if you are running web_app version 3.0 and have the Servlet 3.0 API, you can directly get the ServletContext HttpServletRequest object object, as shown below:

 <% ServletContext sc = request.getServletContext(); sc.getAttribute("attributeName"); %> 

but you should use the application object when using a scriptlet to get the JSTL application object it is much better to use the script code

More details:

+14
source

Source: https://habr.com/ru/post/974685/


All Articles