Configuring a small service program with an XML small service program configuration file

Source: Internet
Author: User
Tags contains http request version
Xml| program uses XML applet configuration file to configure small service programs
This alternative small service program configuration approach involves creating an XML applet configuration file (which is an XML document with the name Servlet_instance_name.servlet), which includes:

File name of the small Service program class file
Small Service Program Description
Small Service Program Initialization parameters
A list of pages that contain the URL (Uniform Resource Locator) of the JavaServer pages (JSPs) that the applet can invoke. The list of pages can include a default page, an error page, one or more mounted target pages (if their names appear in the HTTP request).
In application Server version 3, the. servlet file is stored in the application's classpath.

When application Server receives a request for an instance of a small service program, it obtains the configuration information for that applet in its. servlet file. In order to invoke the page-list element of the Jsp,xml applet configuration file and the Pagelistservlet class (discussed in the next section), the URL hard-coded to the JSPs called is eliminated. If the referenced page changes, you only need to update the. servlet file without having to update the small Service program code and recompile the small service program. Whenever the. servlet file changes, the application Server automatically loads the small service instance.

WebSphere Application Studio provides wizards for building small service programs. These wizards create an XML applet configuration file (that is, a. servlet file) for each Studio-generated small service program. If you do not have access to Studio and you want to manually implement the XML applet configuration:

Create a small service program that extends the Pagelistservlet class.
XML Applet configuration file to create a small service program instance using the Xmlservletconfig class
Expand the compiled small service program and its XML applet configuration file on the application Server.
Using the Pagelistservlet class
Pagelistservlet are super classes of small service programs that load the pages contained in the <page-list> element of the XML applet configuration file. The list of pages allows you to avoid hard-coded the URI of the called JSP. The list can contain a default page, an error page, and other JavaServer pages that are invoked based on HTTP requests.

Pagelistservlet contains a callpage () method that invokes a JavaServer page as a response to an HTTP request for a page in the page list. When you enter a page name in the page-list element of an XML configuration file, the Pagelistservlet.callpage () method receives the HttpServletRequest object and the HttpServletResponse object.

The Pagelistservlet.callpage () method is invoked without exiting. All the small Service program code immediately following the Callpage () method call will run after the call. Consider this when you are architecting your small Service program code.

Simplepagelistservlet is an example of a small service program that extends the Pagelistservlet class and uses its Callpage () method to invoke a JSP:

public class Simplepagelistservlet extends Com.ibm.servlet.PageListServlet {
public void Service (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException
{
try{
Setrequestattribute ("TestVar", "Test Value", req);
Setrequestattribute ("OtherVar", "other value", req);

String pagename = getpagenamefromrequest (req);
Callpage (pagename, req, resp);
}
catch (Exception e) {
HandleError (req, RESP, e);
}
}
}

Using the Xmlservletconfig class
Use the Xmlservletconfig class to create an XML small service configuration file (Servlet_instance_name.servlet). The following is a configuration file:

<?xml version= "1.0"?>
<servlet>
<code>SimplePageListServlet</code>
<description>shows to use Pagelistservlet class</description>
<init-parameter name= "name1" value= "value2"/>
<page-list>
<default-page>
<uri>/index.jsp</uri>
</default-page>
<error-page>
<uri>/error.jsp</uri>
</error-page>
<page>
<uri>/TemplateA.jsp</uri>
<page-name>page1</page-name>
</page>
<page>
<uri>templateB.jsp</uri>
<page-name>page2</page-name>
</page>
</page-list>
</servlet>

Element <servlet> is the basic element of an XML configuration file. Each XML configuration file must be a well-formed XML document. These files are not validated against the document type definition (DTD).

In order for application Server to load a small service program instance with an XML applet configuration file, it must contain at least the code element. For pagelistservlet,xml configuration files, you must have at least the code element and the Page-list element. Although there is no DTD, it is recommended to display all elements in the order in which the elements appear in the example. These elements (also known as tags) are:

Servlet
Basic elements. The element is automatically generated by the Xmlservletconfig class.

Code
Class name for the small Service program (No. class extension), even if the applet is in a JAR file

Describe
User-defined Small service program description

Init-parameter
The attribute of the element specifies a name-value pair that is the initialization parameter. A small service program can have multiple initialization parameters, each with its own init-parameter element.

Page-list
It identifies the JavaServer Page to invoke, based on the path information in the HTTP request. The Page-list element can contain the following child elements:

Default-page: If the HTTP request contains no path information, then the element contains a URI element that indicates where the page is loaded
Error-page: If the HandleError () method sets the request property to "error", the element contains a URI element that indicates where the page is loaded
Page: If the HTTP request contains a page name, then the element contains a URI element that indicates where to load the page. A page-list element can contain multiple page elements.
You can write a Java program that uses the Xmlservletconfig class to generate a small service program configuration file. The Xmlservletconfig class provides methods for setting and getting file elements and their contents.

Expand the small service programs and. servlet files on the server
To use a compiled small service program and its XML applet configuration file on the application Server:

Place the compiled small service program and its. servlet files in the application class path. If the applet has a package name, make sure that the applet is placed in the correct subdirectory of the classpath.
Make sure that the JavaServer page (JSP file) referenced in the page list is located in the document root of the application.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.