Monday, November 14, 2005

Why can’t we access Servlet initParameter in EL like we do in Scriptlets?
<% request.getInitParameter(“name”); %>

Why this is giving error?
${pageContext.request.initparameter.name}
When we can access context initParameter like
${initParam.name} why can’t we do the same with Servlet initParameter.


You can not get Servlet int Parameters using this technique. The getInitParameter (param) does not fit in this case, because it require some arguments.

According to the Javabean spec. the property have getter setter methods in the form

public type1 getXXX() -- WITH NO ARGUMENTS.
public void setXXX(type1)

that’s it.

Now Consider the pageContext as bean Object. The PageContext class have methods like getServletContext(), getRequest(), getSession() etc. You can access these like pageContext.request, pageContext.servletContext etc in EL.

These SevletContext object have some 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.

My suggestion, if you want access to the initParams for the controller servlet, is to create a Map of the init params for the servlet and place it on the request as a scoped variable -- let's say named initParameters. You would then be able to obtain any param by name with ${requestScope.initParameters.name}.

0 Comments:

Post a Comment

<< Home