tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject Re: How to pass init parameter to a JSP
Date Thu, 26 Jul 2001 06:29:51 GMT
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
      


Mime
View raw message