example.servlet.basic
Class InitServlet

java.lang.Object
  extended byHttpServlet
      extended byexample.servlet.basic.InitServlet

public class InitServlet
extends HttpServlet

Shows how to get initialization parameters from the configuration file.

The following is a sample configuration for the examples. It uses Resin's short syntax which is more scannable and maintainable.

 <web-app>

 <context-param context-a='context-value-a'/>

 <servlet servlet-name='servlet-a'
          servlet-class='example.servlet.basic.InitServlet'>
   <init-param init-a='value-a'/>
 </servlet>

 <servlet servlet-name='servlet-b'
          servlet-class='example.servlet.basic.InitServlet'>
   <init-param init-a='value-b'/>
 </servlet>

 <servlet-mapping url-pattern='/servlet/*'
                  servlet-name='invoker'/>

 </web-app>
 

The specific servlet instance depends on the URL. In the above example, there are three URLs that will use the InitServlet, but all three will use different servlet instances.

The example includes a count variable so you can see how the servlet instances are reused. Since Resin only creates a single servlet instance, servlet programmers must be aware of threading issues.

See Also:
Serialized Form

Constructor Summary
InitServlet()
           
 
Method Summary
 void doGet(HttpServletRequest request, HttpServletResponse response)
          Handles GET requests.
 void init()
          The init() method is called when the servlet is initialized.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InitServlet

public InitServlet()
Method Detail

init

public void init()
          throws ServletException
The init() method is called when the servlet is initialized. For most servlets, it will be called on the first request to the servlet. For load-on-startup servlets, it will be called when the web-app initializes.

Servlets normally use init() to cache initial lookups. A typical example is caching a lookup of a database DataSource or an EJB home.

Throws:
ServletException

doGet

public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
           throws java.io.IOException,
                  ServletException
Handles GET requests. Resin will call the doGet method when the browser sends a GET request.

Parameters:
request - the request object contains the data from the browser's request.
response - the response object contains methods to send data back to the browser.
Throws:
java.io.IOException
ServletException