External Interfaces > Reality Java Interface > Writing Servlets to Access Reality via RealWeb > Example Servlet Code

Comment on this topic

Documentation Comments

Use this form to comment on this topic. You can also provide any general observations about the Online Documentation, or request that additional information be added in a future release.

Reality V15.0 ()

Example Servlet Code (JReal) (exampleservlet.htm)

To

Reality

Version

Topic

Submitted by

Company

Location

Email address

Comment

 

 

Example Servlet Code

/* @(#) rwdemo.java ver 22.4 00/08/21 14:43:49
 * ???.java, initial version May 2000
 *
 * Copyright (c) 2000 Northgate Information Solutions Plc.
 * All Rights Reserved.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.northgateis.reality.rsc.*;
import com.northgateis.realweb.*;
/**
 * Sample servlet demonstrating access to a Reality database.
 *
 * @author Kevin Wright
 */
public class rwdemo extends HttpServlet
{
   private static String NL = "<BR>"; 
   
   public void init(ServletConfig config) throws ServletException {
      super.init(config);
      /* Must initialise the RealWeb connection timer */
      RealWeb.init();
   }
   /**
    * Handle post requests by by calling doGet()
    */
   public void doPost(HttpServletRequest req, HttpServletResponse res)
                      throws ServletException, IOException {
      doGet(req, res);
   }
   /**
    * This is the main entry point for a servlet and handles get requests
    * from a browser
    */
   public void doGet(HttpServletRequest req, HttpServletResponse res)
                       throws ServletException, IOException {
      PrintWriter out;
      RealWebUrl URL;
      RealWeb connection = null;
      // set content type
      res.setContentType("text/html");
      // Get an output stream
      out = res.getWriter();
      // Create an HTML page header
      out.println("<HTML>");
      out.println("<HEAD>");
      out.println("<TITLE>RealWeb Demo Servlet</TITLE>");
      out.println("</HEAD>");
      out.println("<BODY>");
      out.println("<H1>RealWeb Demo Servlet</H1>");
      try {
          // Generate a RealWebUrl object to extract the required
          information from the URL
          URL = new RealWebUrl(req);
          // Get a connection (new or current)
          connection = RealWeb.getConnection(req, res, URL);
          // Run the BASIC program
          self.execute(req, res, connection, URL, out);
      }
      catch (IOException e) {
          // Handle exceptions by just printing a message
          out.println(e.toString());
      }
      finally {
          // Must always make the connection available again
          if (connection != null)
              connection.setAvailable();
      }
      // Finish the HTML page
      out.println("</BODY>");
      out.println("</HTML>");
 }
   public String getServletInfo() {
       return "Sample RealWeb servlet";
   }
    /**
     * Execute a Remote Basic subroutine and send the results
     * back to the browser
     *
     * Create, compile and catalog the following subroutine
     * The subroutine just returns its input parameters.
     *
     SUBROUTINE RW.SAMPLE(PARAM1, PARAM2)
     PARAM1 = "First parameter is ":PARAM1
     PARAM2 = "Second parameter is ":PARAM2
     RETURN
     *
     * Don't forget to enable the subroutine by creating a V entry in the
     * Remote Basic security file (RBASIC by default)
     *
     * Use the following URL to access the subroutine
     * /host/servlet/rwdemo/section/RW.SAMPLE?a query
     *
     * Where host is the host running the web server
     * and section is a section name generated by the rwconfig servlet
     */
    private void execute(HttpServletRequest req,
                        HttpServletResponse res,
                        RealWeb con,
                        RealWebUrl URL,
                        PrintWriter out)
                throws RSCException, IOException
   {
         // Attempt to run the Remote Basic program
         try
         {
              //Get the subroutine name from the processed URL
              String subName = URL.command;
              //Create a subroutine handle
              RSC sub = new RSC(con, subName);
              String query = req.getQueryString();
              if (query == null)
                  query = "none";
              RSCParam p1, p2;
              
              // Get references to the parameters
              p1=sub.getParam(1);
              p2=sub.getParam(2);
              // Set the parameters
              p1.setValue(query);
              p2.setValue(req.getServletPath());
              
              // Run the Remote Basic program
              sub.execute();
              // Print the returned parameters, one per line
              out.println(p1.getString());
              out.println("<BR>");
              out.println(p2.getString());
            }
            catch (RSCException e) {
              // catch any RSC exceptions and convert them to HTML
              throw new RealWebException(NL+RealWeb.AMtoNL(e.toString())+NL);
            }
      }
      public void destroy() }
            RealWeb.destroy ();
            super.destroy
      }
  }

RealityV15.0Comment on this topic