/* * @(#)HelloWorld * * Copyright (c) 1999 Karl Moss. All Rights Reserved. * * You may study, use, modify, and distribute this software for any * purpose provided that this copyright notice appears in all copies. * * This software is provided WITHOUT WARRANTY either expressed or * implied. * * @author Karl Moss * @version 1.0 * @date 17Feb99 * */ package javaservlets.nativeCode; import javax.servlet.*; import javax.servlet.http.*; /** * This servlet uses native code to get a "Hello World" message */ public class HelloWorld extends HttpServlet { /** * Use a static initializer to load the native code which * is contained within a library. This initializer is * called when the class loader first loads this class. */ static { System.out.println("Loading HelloWorld Library"); System.loadLibrary("HelloWorld"); } /** *

Performs the HTTP GET operation * * @param req The request from the client * @param resp The response from the servlet */ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { // Set the content type of the response resp.setContentType("text/html"); // Get the PrintWriter to write the response java.io.PrintWriter out = resp.getWriter(); // Create the header out.println(""); out.println(""); out.println("Hello World Using Native Code"); out.println(""); out.println("

"); out.println("Native code returning: " + getMessage()); out.println("

"); out.println("Native code inserts: "); printMessage(out); // Wrap up out.println(""); out.println(""); out.flush(); } /** * Gets a message from a native library * @return A message */ public native String getMessage(); /** * Prints a message to the print stream * @param out The print stream */ public native void printMessage(java.io.PrintWriter out); /** *

Initialize the servlet. This is called once when the * servlet is loaded. It is guaranteed to complete before any * requests are made to the servlet * * @param cfg Servlet configuration information */ public void init(ServletConfig cfg) throws ServletException { super.init(cfg); } /** *

Destroy the servlet. This is called once when the servlet * is unloaded. */ public void destroy() { super.destroy(); } }