Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/voytechs/example/helloworld/HelloWorldServlet.java


1   /*
2    * File: HelloWorldServlet.java
3    * Auth: Mark Bednarczyk
4    * Date: 2002-01-24
5    *   Id: $Id: HelloWorldServlet.java,v 1.1 2002/01/24 20:35:31 voytechs Exp $
6    ********************************************
7    * $Log: HelloWorldServlet.java,v $
8    * Revision 1.1  2002/01/24 20:35:31  voytechs
9    * Documentation and a example application.
10   *
11   */
12  package com.voytechs.example.helloworld;
13  
14  import com.voytechs.html.application.ToolServlet;
15  import com.voytechs.html.component.Panel;
16  import com.voytechs.html.component.Text;
17  import com.voytechs.html.event.EventException;
18  
19  import java.lang.*;
20  import java.util.*;
21  
22  /**
23   * This is a sample application which simply displays HelloWorld message back to the client
24   * browser.<BR>
25   * Here is how we write this application.<BR><BR>
26   * <OL>
27   * <LI> subclass the ToolServlet class and define its abstract method createSessionPanel.</LI>
28   * <LI> In the method createSessionPanel() we create a new panel, add a Text("Hello World!!!") object and return it.
29   * </OL>
30   * To install this application in your servlet environment, for tomcat we do the following. We have to  
31   * define the new servlet in web.xml file and copy the files to the appropriate tomcat directory.
32   * We add to WEB-INF/web.xml file:
33   * <servlet>
34         <servlet-name>HelloWorld</servlet-name>
35        <description>
36        </description>
37        <servlet-class>com.voytechs.example.helloworld.HelloWorldServlet</servlet-class>
38        <!-- Load this servlet at server startup time -->
39        <load-on-startup>5</load-on-startup>
40    </servlet>
41   *
42   */
43  public class HelloWorldServlet 
44    extends ToolServlet {
45    /* Internal attributes */
46  
47    /**
48     * Constructor for our HelloWorld application. We simply need to do this step to define a uniq
49     * name for our application. This isn't really needed by our simple applicaton, but some advanced
50     * functionality utilizes these application names to allow servlets to interface with each other 
51     * , ie. ToolServlet.getToolServlets().
52     * So to be a sport, we need to provide the application name.
53     */
54    public HelloWorldServlet() {
55      super("HelloWorld");
56    }
57  
58  
59  
60    /**
61     * This method is called for every new user session the servlet creates. That is, there is only
62     * 1 HelloWorldServlet object instance, but there can be many Panel object instances.
63     * @return the Panel object which has the GUI we want.
64     * @exception EventException any library errors.
65     */
66    public Panel createSessionPanel() throws EventException {
67  
68      /*
69       * Main container for our little application. It doesn't draw anything itsself
70       * but contains children element which do. Just for clarification this is not the top level
71       * element, this method is called to create the content of the application and this Panel element
72       * will be added to the top-level Frame object created, 1 per user session.
73       */
74      Panel helloWorldPanel = new Panel();
75  
76      /*
77       * In this step we are creating a new Text object giving it a string which will be
78       * output when its called upon to draw itself, and in the same step it is added as a child
79       * to the Panel object.
80       */
81      Text helloText = new Text("Hello World!!!");
82  
83      /*
84       * We add the Text element as a child of our Panel. This and the step above can easily be
85       * done in a single step in Java, but are listed here separately for clarity.
86       */
87      helloWorldPanel.addElement(helloText); 
88  
89      /*
90       * Lastly we return the Panel object which has 1 child, the Text object.
91       */
92      return(helloWorldPanel);
93  
94      /* The invocation of all these elements happens as follows.
95       * 1)  user types in URL: http://localhost/HtmlAWT/servlet/HellowWorld and hits go.
96       * 2) A getRequest method request is called and in the ToolServlet object.
97       * 3) ToolServlet object figures out if this is an existing session or creates a new one.
98       * 4) In our case, this is a new session. So the ToolServlet object creates a new FrameServlet object
99       *    and calls the abstract method createSessionPanel() which we override here.
100      * 5) We create our Panel and Text elements are return the Panel.
101      * 6) ToolServlet adds the Panel we returned as a child of the FrameServlet object created in step #4.
102      * 7) Now the new session is created, the ToolServlet calls on FrameServlet to render itself.
103      * 8) Since FrameServlet is a container, it respectively calls on all its children to render. But first it out
104      *    puts some neccessary HTML header stuff such as HEAD, HTML, BODY and so forth.
105      * 9) We are the only child, a Panel, so we call on our own children to render themselves too.
106      * 10) This calls our Text object. In Text.paint(HtmlWriter out) method it simply say out.text("Hello World!!") passing
107      *     it the string we told it to display.
108      * 11) Since that is a leaf node, the whole thing unwinds itself and back to the top-level FrameServlet object.
109      * 12) There FrameServlet outputs closing tags such as /BODY and /HTML
110      * 13) Lastly we flush the output and we wait for another request.
111      * 14) If the user hits the refresh button, same process will take place, except we already have a session created and
112      *     our FrameServlet object is already there with our Panel and our Text elements. So no new object creation happens,
113      *     but the entire tree is called upon to render itself.
114      */
115 
116      /*
117       * Further notes:
118       * All elements place their output via the HtmlWriter object on the output stream immediately. Only defaul internal stream 
119       * buffering occures. So content is send back to the client almost instanteniously. Your applicaton should try to do the same
120       * so if there is a busy task that the app will perfom, as much output is send to the client as possible. Also things such as
121       * please wait, or percent complete are possible to display on the browser. You are welcome to do any buffering you feel neccessary
122       * by encapsulating the HtmlWriter in other custome writers.
123       * Lastly a flush of the output stream is done before control is returned to the servlet engine.
124       */
125 
126   }
127 
128 } /* END OF: HelloWorldServlet */