example.servlet.basic
Class MappingServlet

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

public class MappingServlet
extends HttpServlet

Shows how the servlet-mapping in the resin.conf and web.xml works.

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

 <web-app>

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

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

 <servlet servlet-name='map-c'
          servlet-class='example.servlet.basic.MappingServlet'>
   <init-param init='value-c'/>
 </servlet>

 <!-- an exact match. -->
 <servlet-mapping url-pattern='/exact'
                  servlet-name='map-a'/>

 <!-- a prefix match. -->
 <servlet-mapping url-pattern='/prefix/*'
                  servlet-name='map-b'/>

 <!-- an extension match. -->
 <servlet-mapping url-pattern='*.map'
                  servlet-name='map-c'/>

 <!-- using the invoker (a prefix match). -->
 <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.

URLServlet PathPath InfoServlet
/exact/exactnullservlet-a
/exact/foon/an/a404 Not Found
/prefix/prefixnullservlet-b
/prefix/foo/prefix/fooservlet-b
/test.map/test.mapnullservlet-c
/exact/test.map/exact/test.mapnullservlet-c
/prefix/test.map/prefix/test.mapservlet-b

The example includes a id 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
MappingServlet()
           
 
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

MappingServlet

public MappingServlet()
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