InfoCenter Home >
4: Developing applications >
4.2: Building Web applications >
4.2.4: Putting it all together (Web applications) >
4.2.4.4: Providing ways for clients to invoke applications >
4.2.4.4.2: Providing Web clients access to servlets >
4.2.4.4.2.1: Invoking servlets within SERVLET tags

4.2.4.4.2.1: Invoking servlets within SERVLET tags

The user can invoke a servlet from an HTML page containing a SERVLET tag.

  This method is not recommended because it only works with JSP .91, and withdrawal of JSP .91 support is imminent.

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 attributes

The 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.

SERVLET attribute Description
NAME It specifies a servlet name, or the servlet class name without the .class extension
CODE It specifies the servlet class name
CODEBASE It specifies a URL on a remote system from which the servlet is to be loaded

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 attributes

In the previous tagging example, parm1 is the name of an initialization parameter and value is the value of the parameter.

PARAM attribute Description
NAME It specifies the name of the parameter for use with this particular invocation of the servlet
VALUE It specifies the value of the parameter for use with this particular invocation of the servlet

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.

Go to previous article: Providing Web clients access to servlets Go to next article: Invoking servlets within JSP files

 

 
Go to previous article: Providing Web clients access to servlets Go to next article: Invoking servlets within JSP files