Place your text ad here.
World class data recovery software and renowned raid recovery services
WestNIC provides reliable web hosting services
Free software downloads and drivers download resources
Ahosting.biz reseller hosting, managed dedicated server with 24/7 support
This FAQ is part of the Code Style Help and FAQ section. Use the help request form below if your question is not answered here, but make sure you are asking the right question first.
init(ServletConfig) method
init(ServletConfig)?
super.init(config)?
init() method?
ServletConfig and ServletContext?
Properties file for my servlet?
destroy() method
A: The servlet lifecycle is a standard sequence of callback events that a servlet container will apply to a servlet to bring it into service and control its shut down. When a servlet is first brought into service, the servlet container will call its init(ServletConfig) method with a servlet configuration object based on its web.xml entry. When the servlet container takes a servlet out of service, it calls the servlet's destroy() method. Between initialisation and destruction, the servlet is in its normal service phase and can handle standard service methods.
More details available to subscribers:
What is the servlet lifecycle?
init(ServletConfig) method
init(ServletConfig)?
A: It is possible to have a custom constructor for a servlet, so long as you also add a default constructor with no arguments, but constructors are not called in the standard servlet lifecycle. Servlets are usually instantiated by the servlet container using the Class.newInstance() method, with no arguments. At this point, the servlet has no reference to its configuration or the general servlet context, so it cannot do any useful start-up activity. These configuration references are only available through the init(ServletConfig) method.
super.init(config)?
A: Servlet programmers are expected to call the super.init(ServletConfig) in the init(ServletConfig) method so that the abstract superclass implementation of the method in GenericServlet stores the servlet configuration object. When the configuration object is stored in this way, the servlet methods getInitParameter(String) and getInitParameterNames() can be called instead of calling the equivalent methods on the configuration object.
init() method?
A: The init(ServletConfig) method is only called once when a servlet is brought into service by the servlet container, not for each new servlet thread. If you create a single database connection in the init(ServletConfig) method and use it to all handle servlet requests, you must ensure all operations are synchronized or you will get unpredictable results. You must also ensure the connection is closed when the servlet is taken out of service by overriding the servlet's destroy() method.
Generally it is better to use the init(ServletConfig) method to register the database driver and get a reference to the DriverManager, a DataSource or database connection pool. Then use the connection provider to get a connection for each request within a try/catch block. This way you can handle the case where the connection fails, and ensure that all connections are closed in any case. The close() method of a pooled Connection instance just returns it to the pool.
A: To pass variable values to a servlet, you should add an initialization parameter to your servlet configuration, see the answer to the question What's the difference between ServletConfig and ServletContext?
More details available to subscribers:
How do I get configuration parameters for a servlet?
ServletConfig and ServletContext?
A: The ServletContext object represents the context for the whole Web application in which a servlet is deployed, and contains initialisation parameters that are shared amongst all servlets in the application. The ServletConfig object represents the configuration for a single specific servlet.
More details available to subscribers:
What's the difference between ServletConfig and ServletContext?
Properties file for my servlet?
A: There are standard methods for identifying and loading this type of static property set into servlets via the ServletContext object. The primary method is getResourceAsStream(String path), which opens an InputStream to the path given in the argument. The path must begin with a forward slash, /, and is relative to the context root, so for instance you might use the path:
/WEB-INF/servlet.properties
More details available to subscribers:
How can I load a Properties file for my servlet?
destroy() method
destroy() method?
A: Any Java method may be overloaded, but the overloaded version will not be called directly by a servlet container's lifecycle methods. The servlet container expects all servlets to have a standard no argument destroy() method, so your servlet is safe to use destroy methods with other signatures.
public void destroy(final String arg) {
// Overloaded method body
}
destroy() from the service() method?
A: The destroy() method has a special significance in the servlet lifecycle when it is called by the servlet container, but is a normal method in all other respects. If you have a typical destroy() method that does servlet clean-up work before the servlet is taken out of service, it is difficult to imagine why you would want to call it from the service() method, though it is possible. Normally, you should handle service requests by overriding the relevant doGet() or doPost() method.
destroy() from init()?
A: The destroy() method is usually called by the servlet container immediately before it takes a servlet out of service. It is typically used to clean-up any resource references, save temporary data and suchlike. If the destroy() method is called from the init(ServletConfig) method, whatever statements are contained in the method will be executed and the method will return, it will not affect the lifecycle status of the servlet from the container's point of view. If the init(ServletConfig) method returns without exception, the servlet container will put the servlet into service regardless.
If an exception is thrown in your init(ServletConfig) method, it may be appropriate to use the destroy() method to help handle the case. If so, you should still throw a ServletException to ensure the servlet is not put into service in this state.
| Front-end FAQs | Back-end FAQs | Learning Java |
|---|---|---|
See site help for questions about this site, our text ads and sponsored links services.