Interface Servlet
- All Known Subinterfaces:
HttpJspPage,JspPage
- All Known Implementing Classes:
GenericServlet,HttpServlet
public interface Servlet
This is the interface for all servlets.
Servlets handle server request.
Servlets have 5 phases in their lifespan, as follows:
- Creation
This is an ordinary constructor call by the server. - init
The server who created the servlet calls theinitmethod somewhere between creation and the first request it ever gives the servlet to handle. - service
For every incoming request the server calls theservicemethod. The server packages all the request data in a ServletRequest object, and creates a ServletResponse object for the servlet to write reply data to.
Note that the service method is run in a seperate thread.
This is also the great advantage of using servlets versus traditional cgi scripting: instead of forking of a proces for every request only a new thread is created. - destroy
This method is called by the server indicating that the server no longer requires this servlet's services. The serlvet is expected to release any resources it is holding using this method.
(With resources things like database connections etc are meant). - Destruction
This happens whenever the garbage collector happens to feel like reclaiming the memory used by this servlet.
- Since:
- Servlet API 1.0
- Version:
- Servlet API 2.2
-
Method Summary
Modifier and TypeMethodDescriptionvoiddestroy()Called by the server when it no longer needs the servlet.Gets the servlet config class.Gets a string containing information about the servlet.voidinit(ServletConfig config) Initializes the servlet.voidservice(ServletRequest request, ServletResponse response) Called by the server every time it wants the servlet to handle a request.
-
Method Details
-
init
Initializes the servlet. Called by the server exactly once during the lifetime of the servlet. This method can be used to setup resources (connections to a database for example) for this servlet. The servlet should store theServletConfigso it can return it again when thegetConfig()method is called. If the the servlet is temporarily or permanently unavailable it should throw anUnavailableException.- Parameters:
config- This servlet configuration class- Throws:
ServletException- If an unexpected error occursUnavailableException- If servlet is temporarily or permanently unavailable- Since:
- Servlet API 1.0
- See Also:
-
service
Called by the server every time it wants the servlet to handle a request. The servlet engine doesn't have to wait until the service call is finished but can start another thread and call the service method again to handle multiple concurrent requests. If a servlet doesn't want this to happen it has to implement theSingleThreadModelinterface.- Parameters:
request- all the request informationresponse- class to write all the response data to- Throws:
ServletException- If an error occursIOException- If an error occurs- Since:
- Servlet API 1.0
- See Also:
-
destroy
void destroy()Called by the server when it no longer needs the servlet. The servlet programmer should use this method to free all the resources the servlet is holding.- Since:
- Servlet API 1.0
-
getServletConfig
ServletConfig getServletConfig()Gets the servlet config class. This should be the sameServletConfigthat was handed to theinit()method.- Returns:
- The config class
- Since:
- Servlet API 1.0
-
getServletInfo
String getServletInfo()Gets a string containing information about the servlet. This String is provided by the Servlet writer and may contain things like the Servlet's name, author, version... stuff like that.- Since:
- Servlet API 1.0
-