InfoCenter Home > 4.2.4.4.2.1: Invoking servlets within SERVLET tagsThe user can invoke a servlet from an HTML page containing a SERVLET tag.
The application developer includes a SERVLET tag in an HTML page to cause a server-side include in which everything between and including the two SERVLET tags is overlaid with the output from the servlet called within the tags. As the name suggests, all processing occurs on the server. The resulting HTML page is sent to the user. The following HTML fragment shows how to use the SERVLET tag: <SERVLET NAME="myservlet" CODE="myservlet.class" CODEBASE="url" initparm1="value"> <PARAM NAME="parm1" VALUE="value"> </SERVLET> Servlet attributesThe loaded servlet will assume a servlet name matching the name specified in the NAME attribute. Subsequent requests for that servlet name will invoke the same servlet instance.
Using the NAME and CODE attributes provides flexibility. Either or both can be used. CODEBASE is optional. With the product, it is recommended that you specify both NAME and CODE, or only NAME if the NAME specifies the servlet name. If only CODE is specified, an instance of the servlet with NAME=CODE is created. If both NAME and CODE are present, and NAME specifies an existing servlet, the servlet specified in NAME is always used. Because the servlet creates part of an HTML file, the application developer will probably use a subclass of the HttpServlet class when creating the servlet and override the doGet() method, because GET is the default method for providing information to the servlet. Another option is to override the service() method. Parameter attributesIn the previous tagging example, parm1 is the name of an initialization parameter and value is the value of the parameter.
You can specify more than one set of name-value pairs. Use the getInitParameterNames() and getInitParameter() methods of the ServletConfig object (which the servlet engine passes to the servlet's init() method) to find a string array of parameter names and values. In the example, parm1 is the name of a parameter that is set to value after the servlet is initialized. Because the parameters set using the <PARAM> tag are available only through methods of a Request object, the server must have invoked the servlet service() method, passing a request from a user. To get information about the user's request, the application developer should use the getParameterNames(), getParameter(), and getParameterValues() methods. The parameters set within the <PARAM> attribute are for a specific invocation of the servlet. If a second JSP file invokes the same servlet with no <PARAM> parameters, the <PARAM> parameters set by the first invocation of the servlet are not available to the second invocation of the servlet. In contrast, servlet initialization parameters are persistent. Suppose a client invokes the servlet by invoking an JSP file containing some initialization parameters. Assume that a second client invokes the same servlet through a second JSP file, which does not specify any initialization parameters. The initialization parameters set by the first invocation of the servlet remain available and unchanged through all successive invocations of the servlet through any other JSP file. The initialization parameters cannot be reset until after the destroy() method has been called on the servlet. For example, if another JSP file specifies a different value for an initialization parameter but the servlet is already loaded, the value is ignored.
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|