Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## javax.servlet
Interface Servlet

All Known Subinterfaces:
  HttpJspPage, JspPage

All Known Implementing Classes:
  GenericServlet, JavaScriptPageServlet

public interface Servlet

A servlet is any Java class with a null-arg constructor that implements the Servlet API.

Simple servlets should extend HttpServlet to create servlets.

Servlets that need full control should extend GenericServlet.

#### Location

Servlets are usually loaded from WEB-INF/classes under the application's root. To add a servlet test.MyServlet, create the java file:
/www/myweb/WEB-APP/classes/test/MyServlet.java

Servlets can also live in the global classpath.

#### Configuration

Servlet configuration for Resin is in the resin.conf file.

     <servlet servlet-name='hello'
              servlet-class='test.HelloServlet'
              load-on-startup>
       <init-param param1='value1'/>
       <init-param param2='value2'/>
     </servlet>

#### Dispatch

The servlet engine selects servlets based on the `servlet-mapping` configuration. Servlets can use the special 'invoker' servlet or they can be configured to execute directly.

To get a path info, your servlet needs to use a wildcard. In the following example, /Hello will match the 'hello' servlet, but /Hello/there will match the 'defaultServlet' servlet with a pathinfo of /Hello/there.

     <servlet-mapping url-pattern='/'
                      servlet-name='defaultServlet'/>

     <servlet-mapping url-pattern='/Hello'
                      servlet-name='hello'/>

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

     <servlet-mapping url-pattern='*.jsp'
                      servlet-name='com.caucho.jsp.JspServlet'/>

#### Life cycle

Servlets are normally initialized when they are first loaded. You can force loading on startup using the 'load-on-startup' attribute to the servlet configuration. This is a useful technique for the equivalent of the global.jsa file.

A servlet can count on having only one instance per application (JVM) unless it implements SingleThreadedModel.

Servlet requests are handed by the `service` routine. Since the servlet engine is multithreaded, multiple threads may call `service` simultaneously.

When the application closes, the servlet engine will call `destroy`. Note, applications always close and are restarted whenever a servlet changes. So `init` and `destroy` may be called many times while the server is still up.

Method Summary
`void` | `destroy()` Called when the servlet shuts down.
`ServletConfig` | `getServletConfig()` Returns the servlet configuration, usually the same value as passed to the init routine.
`java.lang.String` | `getServletInfo()` Returns an information string about the servlet.
`void` | `init(ServletConfig config)` Initialize the servlet.
`void` | `service(ServletRequest req, ServletResponse res)` Service a request.

Method Detail

### getServletInfo

    public java.lang.String getServletInfo()

  Returns an information string about the servlet.

### init

    public void init(ServletConfig config)
              throws ServletException

  Initialize the servlet. ServletConfig contains servlet parameters from the configuration file. GenericServlet will store the config for later use.
  Parameters:
    `config` - information from the configuration file.

### getServletConfig

    public ServletConfig getServletConfig()

  Returns the servlet configuration, usually the same value as passed to the init routine.

### service

    public void service(ServletRequest req,
                        ServletResponse res)
                 throws java.io.IOException,
                        ServletException

  Service a request. Since the servlet engine is multithreaded, many threads may execute `service` simultaneously. Normally, `req` and `res` will actually be `HttpServletRequest` and `HttpServletResponse` classes.
  Parameters:
    `req` - request information. Normally servlets will cast this to `HttpServletRequest`
    `res` - response information. Normally servlets will cast this to `HttpServletRequest`

### destroy

    public void destroy()

  Called when the servlet shuts down. Servlets can use this to close database connections, etc. Servlets generally only shutdown when the application closes.

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD