public interface Servlet
Simple servlets should extend HttpServlet to create servlets.
Servlets that need full control should extend GenericServlet.
Servlets can also live in the global classpath.
<servlet servlet-name='hello'
servlet-class='test.HelloServlet'
load-on-startup>
<init-param param1='value1'/>
<init-param param2='value2'/>
</servlet>
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'/>
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.
Modifier and Type | Method and Description |
---|---|
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.
|
java.lang.String getServletInfo()
void init(ServletConfig config) throws ServletException
config
- information from the configuration file.ServletException
ServletConfig getServletConfig()
void service(ServletRequest req, ServletResponse res) throws java.io.IOException, ServletException
service
simultaneously. Normally,
req
and res
will actually be
HttpServletRequest
and HttpServletResponse
classes.req
- request information. Normally servlets will cast this
to HttpServletRequest
res
- response information. Normally servlets will cast this
to HttpServletRequest
java.io.IOException
ServletException
void destroy()