The Java Server is both flexible and extensible. Using the Java Server APIs, you can write your own servlet and incorporate it into the server. To do this, follow these three steps:
import java.io.*; import javax.servlet.*; public class HelloServlet extends GenericServlet { public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { PrintStream out = new PrintStream(res.getOutputStream()); out.println("Hello World!"); } public String getServletInfo() { return "Hello World Servlet"; } }
After creating this source code in your favorite editor, compile this example servlet, and place the classfile in the server_root/servlets directory on your server. When compiling, don't forget to include the javax.servlet.* package in your classpath. The easiest way to do this is to include server_root/lib/classes.zip in your classpath. For instructions on how to do this, please refer to your compiler's documentation.
In the preceding code, the service method is called every time the servlet is accessed. It writes "Hello world!" to the response, which causes the phrase "Hello World!" to appear on the display of the browser accessing the servlet.
The real beauty of the Java Server is that it is extensible. But, before you can use a servlet to add extended functionality to the Java Server, you have to use the Java Server Administration applet to install the servlet and specify the default parameters and arguments.
Display the Administration Applet by connecting to:
The following instructions assume that you have already logged in, and are using the Servlet Loading facility to add a servlet. To do this, click on the Servlets button, and select Add from the list of choices on the left.
Then, to add a new servlet, fill in the following fields:
To add the HelloServlet.class file located in the servlets directory, enter hello for the servlet name, and HelloServlet for the servlet class. Click on the Add button.
To then map the HelloServlet to a URL, go to the Setup Section, and select Servlet Aliases. In the servlet aliases screen, click on the Add button. Then enter /hello.txt in the URL Pathname, and hello in the Servlet invoked field. Click on save.
The HelloServlet can now be invoked with the URL:
The syntax for the servlet tag is:
<servlet name=ServletName code=ServletCode.class initParam1=initArg1 initParam2=initArg2 ...> <param name=param1 value=val1> <param name=param2 value=val2> . . . </servlet>
The server first tries to invoke the servlet with the name referenced by the
name field. If this fails or if no name is provided it attempts to load the
servlet based on the code
field, passing any remaining fields
within the <servlet>
tag to the servlet as initialization
arguments. If the server loads the servlet and a name was specified, the
server keeps the servlet loaded so that the next time the servlet is accessed
it is not reloaded. If no name is specified, the server reloads the servlet
each time it is accessed.
Once the server has a handle to the servlet, either by referencing it from
the name or loading it, it calls service on the servlet. The name value pairs
specified in the html file with the <param>
tag are
accessible to the servlet using the standard getParameter
and getParameters
methods on the ServletRequest object passed
to the servlet in service.
Everything the servlet writes to
ServletResponse.getOutputStream()
gets written
out to the client as part of the document the client requested. The
servlet's output appears in the document at the location where the servlet
tag was embedded.
<HTML><HEAD><TITLE>Hello World SSI</TITLE></HEAD>
<BODY>
<H1>Amazing Server Side Include Text Follows:</H1>
<servlet name=hello>
</servlet>
</BODY></HTML>