Tims Servlet Tutorial

Tutorial Home

  1. What are servlets ?
  2. The Hello World Servlet
  3. Running Servlets
  4. Reading Form Data
  5. Servlet Configuration
  6. Client Side Form Validation
  7. Cookies
  8. Sessions
  9. Authentication
  10. Dynamic Images
  11. Design, Testing, Security
  12. Servlets and Apache
  13. Using Servlets in SOCS

Servlet Configuration


When a Servlet is first started by the servletrunner or a webserver, it can be passed a set of parameters to the servlet. These parameters are stored in a file which is accessed by your servlet engine. The format varies for different servlet engines.


JSDK 2.0 : Servlet Runner & Apache JServ

Under the servletrunner, these parameters can be specified in a file called servlet.properties, such as that shown below. Under apache, the name of the file will depend on your configuration, but is likely to be servlet.properties or zone.properties.

servlet.MyNameServlet.initArgs=MESSAGE=Hello

If the servlet.properties file is placed in the same directory as the root of the servlet tree, then it will be read automatically when the servletrunner is invoked. Alternatively, you can use the -s tag to explicitly tell the servletrunner where to find the properties. eg :

servletrunner -d /home/tmw/myservlets -s /home/tmw/myservlets/servlet.properties


JSDK 2.1 : JSDK Server

The JSDK server reads all the config parameters from the WEB-INF/servlets.properties file. In order to use properties, the servlet has to be 'mapped' and then the mapped servlet can be given parameters. eg :

name.code=MyNameServlet
name.initparams=MESSAGE=Hello

This will set up a mapped servlet called 'name', when this servlet is accessed through the URL 'http://localhost:5000/servlet/name' (or equivalent), it will be passed the parameters specified in the initparams property. If you try to access the servlet directly (ie with the class name), these parameters will be ignored.


What happens ?

For the above examples, when the servlet engine is invoked, a parameter called 'MESSAGE' with the value 'Hello' is sent to the MyNameServlet, packaged as a ServletConfig object. All Servlet enabled webservers should be capable of passing startup parameters in the same way. In order to recieve this data, the servlet needs to implement an additional method which is used to receive this configuration information. This modification is shown below.

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

public class MyNameServlet extends HttpServlet
{
 private String message;

 /**
 * Servlet method to initialise the class. Receives data from the servlet.properties file.
 * @param conf a ServletConfig object describing the default startup params for this class
 **/

 public void init(ServletConfig conf) throws ServletException
 {
  message=conf.getInitParameter("MESSAGE");	//  Read the init parameter MESSAGE
 }

 /**
 * Method to receive get requests from the web server
 * (Passes them onto the doPost method)
 * @param req The HttpServletRequest which contains the information submitted via get
 * @param res A response containing the required response data for this request
 **/

 public void doGet(HttpServletRequest req, HttpServletResponse res)
   throws ServletException, IOException
 {
  doPost(req,res);
 }

 /**
 * Method to receive and process Post requests from the web server
 * @param req The HttpServletRequest which contains the information submitted via post
 * @param res A response containing the required response data for this request
 **/

 public void doPost(HttpServletRequest req, HttpServletResponse res)
   throws ServletException, IOException
 {
  // Read the value of the 'yourname' parameter
  String name=req.getParameterValues("yourname")[0]; 

  //*****Construct a response in HTML*****
  String reply="<HTML>\n"+
   "<HEAD><TITLE>My Name Servlet Response</TITLE></HEAD>\n"+
   "<BODY>\n"+
   "<CENTER><BR><B>\n"+
   message+" "+name+"\n"+
   "</B></CENTER>\n"+
   "</BODY>\n"+
   "</HTML>";

  //*****Send the reply*****

  res.setContentType("text/html");
  PrintWriter out=res.getWriter();
  out.println(reply);
  out.close();
 }
}


This servlet adds the 'init' method which receives the 'ServletConfig' object. The parameters held within this object can then be simply read with the 'getInitParameter' method.


<< Previous Page | Next Page >>




Tims Home Page       |       Page last modified : 19 May 2003