I generally do it two ways, depending if parameters have to be dynamically
modified. Here's an example comparing the to ways.

For tomcat, web.xml is a little different Nic's example (the difference is
in <init-param> syntax) :

<servlet>
        <servlet-name>servlet1</servlet-name>
        <servlet-class>AServlet</servlet-class>
        <init-param>
                <param-name>param</param-name>
                <param-value>This is init parameter value for servlet 1</param-value>
        </init-param>
</servlet>
<servlet>
        <servlet-name>servlet2</servlet-name>
        <servlet-class>AServlet</servlet-class>
        <init-param>
                <param-name>param</param-name>
                <param-value>This is init parameter value for servlet 2</param-value>
        </init-param>
</servlet>

<servlet-mapping>
        <servlet-name>
                servlet1
        </servlet-name>
        <url-pattern>
                /servlet1/*
        </url-pattern>
</servlet-mapping>

<servlet-mapping>
        <servlet-name>
                servlet2
        </servlet-name>
        <url-pattern>
                /servlet2/*
        </url-pattern>
</servlet-mapping>

Now, the following servlet use two different ways to access different
parameters based on different names :

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class AServlet extends HttpServlet {

        String defaultPropFileName = "aservlet.prop";

        public void doGet (HttpServletRequest request,
                                HttpServletResponse response) throws ServletException, 
IOException{

                String path = getServletConfig().getServletContext().getRealPath("/");
                response.setContentType("text/html");
                PrintWriter pw = response.getWriter();

                String initparam = getServletConfig().getInitParameter("param");
                String name = getServletConfig().getServletName();

                String propFileName = request.getParameter("propfile");
                if (propFileName == null || propFileName.equals("")) {
                        propFileName = defaultPropFileName;
                }
                Properties properties = new Properties();
                try {
                        FileInputStream propFile = new FileInputStream(path + 
propFileName);
                        properties.load(propFile);
                }
                catch (IOException ioe) {
                }
                String propparam = properties.getProperty(name);
                pw.println("<p>initparam = " + initparam);
                pw.println("<p>propparam = " + propparam);
        }
}

The prop file contents the following data :

servlet1=This is prop parameter value for servlet 1
servlet2=This is prop parameter value for servlet 2

and the file is named "aservlet.prop"

output for URL http://localhost:8080/servlet2/ is :

initparam = This is init parameter value for servlet 1

propparam = This is prop parameter value for servlet 1

output for URL http://localhost:8080/servlet2/ is :

initparam = This is init parameter value for servlet 2

propparam = This is prop parameter value for servlet 2

So the to ways are working the same. The main advantage of using a prop file
is that you can change the prop file without restarting Tomcat. You can
change propfile content (using another servlet, you can do it from a web
browser). You can even pass a prop file name to the servlet as a parameter.

Pierre-Yves

-----Message d'origine-----
De : A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]De la part de Nic
Ferrier
Envoyé : jeudi 22 mars 2001 03:22
À : [EMAIL PROTECTED]
Objet : Re: Startup parameters for a servlet


>>> Mandar Joshi <[EMAIL PROTECTED]> 22-Mar-01 1:45:05 AM >>>

>for example I register a servlet with name
>Servlet1 I want to send a paramter "xyz" to it
>I register a same servlet class with another name
>Servlet2 I want it to take paramter "abc".
>The purpose of the whole exercise is to initialize
>the servlet depending its registered name / url,
>so that I can have some computation based on
>that.

To do exactly what you want can't be achieved.

However, you can do this in your web.xml:

<servlet>
  <servlet-name>fred1</servlet-name>
  <servlet-class>com.Fred.FredServlet</servlet-class>
  <init-param>someparam</init-param>
  <init-value>xyz</init-value>
</servlet>

<servlet>
  <servlet-name>fred2</servlet-name>
  <servlet-class>com.Fred.FredServlet</servlet-class>
  <init-param>someparam</init-param>
  <init-value>abc</init-value>
</servlet>

<servlet-mapping>
  <servlet-name>fred1</servlet-name>
  <url-pattern>/firstfred/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>fred2</servlet-name>
  <url-pattern>/secondfred/*</url-pattern>
</servlet-mapping>


There will be two copies of the servlet in the container, they will
be completely different instances but some communication is possible
using static methods and variables.


Nic

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to