All Examples 

package examples.servlets

Class Index

Basic Servlets...

SimpleServlet (code)
HelloWorldServlet (code)
Two very simple servlets that just respond to an HTTP request with a simple HTML page.
HelloKonaServlet (code)
Another very simple servlet that uses htmlKona to generate the response HTML page.

Retrieving and using HTTP query data...

SurveyServlet (code)
Demonstrates reading input from a form, and saving the details to a file on the server. The servlet is invoked from a form in an HTML file.

You will need to copy the extra "Survey.html" file to the document root of your WebLogic Server, and request it from the web browser, instead of the virtual servlet name directly.

Note that this servlet requires an initArg registration that specifies where to store the survey results. This is set to "/weblogic/myserver" in the example registrations below. You will need to make sure this directory exists and that the WebLogic Server has write permissions to this directory.

SimpleFormServlet (code)
Again, demonstrates using a form, and returning the results, but this time it is implemented using htmlKona to generate the response pages. Unlike the SurveyServlet, this servlet generates its own form when invoked with no query parameters. The returned HTML form references the servlet again to process user input. You can call this servlet directly using it's registered URL.
PhoneServlet (code)
This servlet demonstrates interactive requests by providing a response based on submited HTTP query parameters, which are generated from the use selecting links in the browser.

Tracking Clients...

CookieCounter (code)
Implements a counter for servlet-page hits, counting the total number of hits, and individual-client hits using cookies.
SessionServlet (code)
Demonstrates tracking client user data using an HttpSession. This is a safer way to store client related data than using cookies.

Miscellaneous Examples...

SnoopServlet (code)
Demonstrates all of the information available to a servlet.
AppletServlet (code)
Demonstrates how to include an applet in the response page. You should have an applet ready to test with this example.
ErrorServlet (code)
Demonstrates how to return an HTTP error code as a page response.
PageEvent (code)
Demonstrates using WebLogic Events, sending data about the page hit to any subscribers to a specific event.

about this package

The servlets in this package illustrate basic to advanced concepts about writing HTTP servlets, using the JavaSoft Java Servlet API. Several of the servlets in this package are taken straight from the JSDK2.0 examples. Some of these servlets have been adapted to take advantage of the WebLogic htmlKona API. You can find more examples of how to use htmlKona for constructing HTML pages in the examples/htmlkona directory.

With WebLogic, your servlets can take advantage of all of WebLogic's services. For servlets that use databases via JDBC, check out the servlet examples in the examples/htmlkona directory.

how to use this package

You can compile every example in this directory from the command line, using the following instructions:
  1. Open a new command shell, set up your development environment, as described in the document Setting your development environment.

  2. Change to the .../examples/servlets directory and compile the servlets with the following command line:
      $ javac -d %SERVLET_CLASSES% *.java
    where %SERVLET_CLASSES is an environment variable that is set when you set up your development environment. It points to a directory in the servlet classpath.

  3. Make sure the following entry is in your weblogic.properties file (This entry is included in the default weblogic.properties file that is included with your WebLogic Server distribution.):
    weblogic.httpd.servlet.classpath=c:/weblogic/myserver/servletclasses
    (Where c:/weblogic is the directory containing your WebLogic Server installation.)

  4. Register each servlet in your weblogic.properties file, using this pattern:
      weblogic.httpd.register.virtualName=fullPackageName

    For example, this is how you would register the PhoneServlet. In addition to the servlet itself, we also register an initialization parameter for the PhoneServlet, which gives a path to its data file. (Not all the servlets use initialization parameters)

      weblogic.httpd.register.phone=examples.servlets.PhoneServlet
      weblogic.httpd.register.phone=\
          phonelist=c:/weblogic/src/examples/servlets/phonelist
    (Where c:/weblogic is the directory containing your WebLogic Server installation.)

    You should find a similar section to the listing below in your weblogic.properties file, where the example servlets are registered.

    
    weblogic.httpd.register.phone=examples.servlets.PhoneServlet
    weblogic.httpd.initArgs.phone=\
            phonelist=/weblogic/examples/servlets/phonelist
    weblogic.httpd.register.snoop=examples.servlets.SnoopServlet
    weblogic.httpd.register.cookies=examples.servlets.CookieCounter
    weblogic.httpd.register.error=examples.servlets.ErrorServlet
    weblogic.httpd.register.applet=examples.servlets.AppletServlet
    weblogic.httpd.register.helloWorld=examples.servlets.HelloWorldServlet
    weblogic.httpd.register.helloKona=examples.servlets.HelloKonaServlet
    weblogic.httpd.register.page=examples.servlets.PageEvent
    weblogic.httpd.initArgs.page=\
            imageurl=/weblogic/examples/images/trans.gif
    weblogic.httpd.register.session=examples.servlets.SessionServlet
    weblogic.httpd.register.simple=examples.servlets.SimpleServlet
    weblogic.httpd.register.simpleFormServlet=examples.servlets.SimpleFormServlet
    weblogic.httpd.register.survey=examples.servlets.SurveyServlet
    weblogic.httpd.initArgs.survey=\
            resultsDir=/weblogic/myserver
    

    For more help with registering servlets, see the WebLogic Administrators Guide document, Setting WebLogic properties.

  5. Each example may require some extra set up procedure, given above with the example description. (particularly the SurveyServlet).

  6. In a another command shell, start the WebLogic Server.

  7. Use a browser to call each servlet, with a URL that follows the pattern:
      http://WebLogicURL:WebLogicPort/virtualName
    For example, here's how you load the PhoneServlet in a browser running on the same NT host as your WebLogic Server, on port 7001:
      http://localhost:7001/phone
    There's more information about running the examples in some of the related class documentation, linked to at the top of this page.

there's more . . .

Read more about WebLogic's support of HTTP servlets in the Developers Guide, Using WebLogic HTTP Servlets. There is more about htmlKona in Using htmlKona. You might also be interested in the SSI and JSP examples.