Posted By

sunkist on 07/24/09


Tagged

java


Versions (?)

using context init parameters in web.xml


 / Published in: Java
 

URL: http://www.coderanch.com/t/287799/JSP/java/JSP-Java-class-s-static

Rather than hard-coding values shared between classes/methods/servlets, it's good to use content params in web.xml.

  1. <!-- add context params in your web.xml -->
  2. <web-app ...>
  3. <context-param>
  4. <param-name>ROLE-ADMIN</param-name>
  5. </param-value>admin</param-value>
  6. </context-param>
  7. </web-app>
  8.  
  9.  
  10. USAGE:
  11.  
  12.  
  13.  
  14. <!-- method usage -->
  15.  
  16. public void someMethod(ServletConfig config) {
  17.  
  18. ServletContext application = config.getServletContext();
  19. String adminRole = application.getInitParameter("ROLE-ADMIN");
  20.  
  21. }
  22.  
  23.  
  24. <!-- jsp scriplet usage-->
  25.  
  26. if (userRole.equals(application.getInitParameter("ROLE-ADMIN")))
  27. {
  28. //do something
  29. }
  30.  
  31. <!-- EL usage -->
  32.  
  33. <c:if test="${userRole eq initParam.ROLE-ADMIN}">
  34. do something
  35. </c:if>

Report this snippet  

You need to login to post a comment.