On Tue, 17 Jul 2001, Simon Chan wrote:

> Hi,
> 
> How to pass initial parameters to a JSP?  I know how to pass initial
> parameters to a servlet via web.xml file (<init-param>). However, I
> don't know where to put for JSP.  This is my JSP file
> 
> ...
> <%
>     String c = config.getInitParameter("cfg");
> ...
> 
> 
> Thanks in advance
> 
> 
> skc
> 

In order to do this, you will need to declare a servlet definition for
your JSP page (including the init parameter) like this:

  <servlet>
    <servlet-name>My Page</servlet-name>
    <jsp-file>/mypage.jsp</jsp-file>
    <init-param>
      <param-name>cfg</param-name>
      <param-value>The Parameter Value</param-value)
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>My Page</servlet-name>
    <url-pattern>/foo</url-pattern>
  </servlet-mapping>

Now, when you access the following url (assuming /myapp is the context
path to your web app):

  http://localhost:8080/myapp/foo

the page "/mypage.jsp" will be loaded, and it will have access to the
servlet initialization parameter you have defined above.  Note that you
could set the url pattern to /mypage.jsp if you wanted to, but it is
crucial to actually have a servlet mapping -- otherwise, your page is
handled by the default JSP servlet (which does not have any
application-specific initialization parameters in its servlet definition).

Craig McClanahan
      

Reply via email to