JavaRanch Home    
 
This page:         last edited 30 August 2006         What's Changed?         Edit

Init Params Thru Expression Language   

How to access servlet init parameters using EL?

You cannot use the following syntax to access servlet init parameters:

 ${pageContext.servletConfig.initParameter.name}

You cannot get Servlet init parameters using this technique. The getInitParameter(java.lang.String name) does not fit in this case, because it requires some arguments.

According to the JavaBean spec, the property has getter & setter methods in the form

 public type1 getXXX() -- WITH NO ARGUMENTS.

 public void setXXX(type1)

Now consider the pageContext as bean Object. The PageContext class has methods like getServletConfig(), getRequest(), getSession() etc. You can access these like pageContext.pageConfig, pageContext.request etc in EL.

ServletContext object has a couple of methods like getMajorVersion(), getMinorVersion() with no args. so we can access these methods treating it as properties to sevletContext bean as pageContext.servletContext.majorVersion and pageContext.servletContext.minorVersion.

If you want to access Servlet init parameters using EL, then it is better to create a Map of the init parameters for the servlet and place it in the request as a scoped variable -- let's say initParameters. You would then be able to obtain any param by name with ${requestScope.initParameters.name}.

NOTE:

We can access context init parameters with ${initParam.name}



ScwcdFaq


JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton