0

In web.xml i have

   <context-param>
      <param-name>email</param-name>
      <param-value>test</param-value>
     </context-param>

In jsp I have

 <c:out value= "MUST PRINT EMAIL">  
    </c:out> 
    <c:out value= "${applicationScope.email}">  
    </c:out>  

But this prints only MUST PRINT EMAIL . The email 'test' value is not printed. Why? How to get context-param?

john
  • 647
  • 5
  • 23
  • 53

1 Answers1

1

I think you can't do this directly, you can dump all attributes in application scope by this code

${pageScope}<br> ${requestScope}<br> ${sessionScope} <br> ${applicationScope}

or more verbose

<%
    Enumeration attrNames=request.getServletContext().getAttributeNames();
    while(attrNames.hasMoreElements()){
        String ctxAttribute=(String) attrNames.nextElement();
        out.print("<br> Attribute Name --> "+ctxAttribute+", has value Value --> "+request.getServletContext().getAttribute(ctxAttribute));
    }

out.println("<br><br>");

    attrNames=application.getAttributeNames();
    while(attrNames.hasMoreElements()){
        String ctxAttribute=(String) attrNames.nextElement();
        out.println("<BR> Attribute Name --> "+ctxAttribute+", has value Value --> "+application.getAttribute(ctxAttribute));
    }
%>

If you want to get this parameter such a way, you can put this attribute to application scope when then context is initialized:

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MyContextListener implements ServletContextListener {
    private static final Log logger = LogFactory.getLog(MyContextListener.class);


    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        String email= servletContextEvent.getServletContext().getInitParameter("email");
        logger.info("Use email:" + email);
        servletContextEvent.getServletContext().setAttribute("email", email+"==setbylistener");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

and don't forget to configure it in your web.xml

  <context-param>
      <param-name>email</param-name>
      <param-value>test123</param-value>
  </context-param>

  <listener>
        <listener-class>MyContextListener</listener-class>
  </listener>

Hope this helps.

Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57