Initializing a Servlet when WebLogic Server Starts Overriding the init Method

9-2 Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server

9.1.1 Initializing a Servlet when WebLogic Server Starts

Rather than having WebLogic Server initialize a servlet when the first request is made for it, you can first configure WebLogic Server to initialize a servlet when the server starts. You do this by specifying the servlet class in the load-on-startup element in the J2EE standard Web application deployment descriptor, web.xml. The order in which resources within a Web application are initialized is as follows: 1. ServletContextListeners—the contextCreated callback for ServletContextListeners registered for this Web application. 2. ServletFilters init method. 3. Servlet init method, marked as load-on-startup in web.xml. You can pass parameters to an HTTP servlet during initialization by defining these parameters in the Web application containing the servlet. You can use these parameters to pass values to your servlet every time the servlet is initialized without having to rewrite the servlet. For example, the following entries in the J2EE standard Web application deployment descriptor, web.xml, define two initialization parameters: greeting, which has a value of Welcome and person, which has a value of WebLogic Developer. servlet ... init-param descriptionThe salutationdescription param-namegreetingparam-name param-valueWelcomeparam-value init-param init-param descriptionnamedescription param-namepersonparam-name param-valueWebLogic Developerparam-value init-param servlet To retrieve initialization parameters, call the getInitParameterString name method from the parent javax.servlet.GenericServlet class. When passed the name of the parameter, this method returns the parameters value as a String.

9.1.2 Overriding the init Method

You can have your servlet execute tasks at initialization time by overriding the init method. The following code fragment reads the init-param tags that define a greeting and a name in the J2EE standard Web application deployment descriptor, web.xml: String defaultGreeting; String defaultName; public void initServletConfig config throws ServletException { if defaultGreeting = getInitParametergreeting == null defaultGreeting = Hello; if defaultName = getInitParameterperson == null defaultName = World; } Servlet Programming Tasks 9-3 The values of each parameter are stored in the class instance variables defaultGreeting and defaultName. The first code tests whether the parameters have null values, and if null values are returned, provides appropriate default values. You can then use the service method to include these variables in the response. For example: out.printbodyh1; out.printlndefaultGreeting + + defaultName + ; out.printlnh1bodyhtml; The init method of a servlet does whatever initialization work is required when WebLogic Server loads the servlet. The default init method does all of the initial work that WebLogic Server requires, so you do not need to override it unless you have special initialization requirements. If you do override init, first call super.init so that the default initialization actions are done first.

9.2 Providing an HTTP Response