You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by chris hutchings <ch...@yahoo.co.uk> on 2001/08/20 16:33:41 UTC

New to servlets2: Parameter initialisation in web.xml

hi, 
the web xml and servlet code are below
-thanks for taking the trouble..

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web
Application 2.3//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

<web-app>

    <!-- Define servlets that are included in the
example application -->
      
    
   <servlet>
       <servlet-name>counter</servlet-name>
       <servlet-class>InitCounter</servlet-class>
		 	<init-param>
	    		<param-name>initial</param-name>
	    		<param-value>1000</param-value>
			</init-param>
    </servlet>	      

</web-app>


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

public class InitCounter extends HttpServlet
{
	int count;
	String  initial;	
	
	public void init() throws ServletException
	{
		initial = getInitParameter("initial");
		try
		{
			count = Integer.parseInt(initial);	
		}
		catch(NumberFormatException e)
		{
			count = 0;
		}
	}
	
	public void doGet(HttpServletRequest req,
HttpServletResponse res) throws ServletException,
IOException
	{
		res.setContentType("text/plain");
		PrintWriter out = res.getWriter();
		
		count++;
		out.println("Since loading, with initialisation");
		out.println("the Servlet has been accessed");	
		out.println(count + " times");
		out.println(initial);		
	}	
}

chris


____________________________________________________________
Do You Yahoo!?
Get your free @yahoo.co.uk address at http://mail.yahoo.co.uk
or your free @yahoo.ie address at http://mail.yahoo.ie