using context init parameters in web.xml


/ Published in: Java
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.