tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chinni.Venkatesw...@epamail.epa.gov
Subject Re: Newbie problem with reading init params
Date Thu, 07 Feb 2002 19:14:56 GMT


You actually read them from ServletContext not from the ServletConfig.

You can get the Servlet Context by doing...

ServletContext sc = getServletContext();


Hope this helps.

-Chinni.




                                                                                         
 
                    Dominik                                                              
 
                    Jednoralski          To:     tomcat-user@jakarta.apache.org          
 
                    <tomcat@lime-        cc:                                          
    
                    design.de>           Subject:     Newbie problem with reading init
    
                                         params                                          
 
                    02/07/02                                                             
 
                    12:02 PM                                                             
 
                    Please                                                               
 
                    respond to                                                           
 
                    Tomcat Users                                                         
 
                    List                                                                 
 
                                                                                         
 
                                                                                         
 




Hey Guys,

I have a problem with getting my init-params from the web.xml-file.
These are the parameters I inserted into the
webapps/root/web-inf/web.xml-file:

<web-app>
  <servlet>
    <servlet-name>ShowMsg</servlet-name>
    <servlet-class>limeservlets.ShowMessage</servlet-class>
    <init-param>
      <param-name>message</param-name>
      <param-value>Nerv-Serv</param-value>
    </init-param>
    <init-param>
      <param-name>repeats</param-name>
      <param-value>5</param-value>
    </init-param>
  </servlet>
</web-app>

Now I failed to read these parameters with the follwing servlet:


package limeservlets;

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

 public class ShowMessage extends HttpServlet {
    private String message;
    private String defaultMessage = "Leider keine Nachricht.";
    private int repeats = 1;

    public void init (ServletConfig config) throws ServletException{
       super.init(config);
       message = config.getInitParameter("message");
       if (message == null) {
                message = defaultMessage;
       }

       try {
                 String repeatString = config.getInitParameter
("repeats");
                 repeats = Integer.parseInt(repeatString);
       } catch (NumberFormatException nfe){}
    }

    public void doGet (HttpServletRequest request, HttpServletResponse
response)
       throws ServletException, IOException{
           response.setContentType("text/html");
           PrintWriter out = response.getWriter();
           String title = "The Servlet which shows a message!";

           out.println(ServletUtilities.headWithTitle(title) +
           "<body bgcolor=#fdf5f6>\n<h1>"+ title + "</h1><br>");
           for (int i = 0; i < repeats; i ++) {
           out.println(message + "<br>");
           }
           out.println("</body></html>");
    }
 }


Does anyone know the reason for that? I for myself don't have a clue-
I'm
not really
into configuring servers...

Dominik Jednoralski

. Lime Design Mediengestaltung
. Lilienstr. 23
. 63768 Hvsbach
. Tel. 06021.550470

. visit www.lime-design.de

--
To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>






--
To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>


Mime
View raw message