AppliWeb release v1.0 User's guide

4. The servlets

4.1 Example

The last release of Appliweb contains a demonstration servlet.
This servlet is called by a Java page which asks his name to the operator. This name is given to the servlet, which adds it dynamically to his response.

You can find below the main source code of the servlet. Numbers in red are links for the explanations below.

  public void performTask(HttpServletRequest request, HttpServletResponse response)
  {
      // We retrieve the name given by the client
1.    String valName = request.getParameter ("NAME");

    try
    {
      // This servlet will load the serialized file specific to the jdk release it will run into
2.    String  responseFile = "/gCollin/appliWeb/test/servlet/ServletOutput1.ser";

      // We create a ContainerHolder and AppliWebBean
3.    ContainerHolder holder = new ContainerHolder (new AppliWebBean (), responseFile);

      // We get the label that will contain the response
4.    JLabel name = (JLabel)holder.getComponentFromName ("NAME", true);
    name.setText(valName);

      // We write the container to the client.
5.    holder.writeContainer (response);
    } catch (Exception e)
    {
    try
      {
        // Write the error message
6.      System.err.println("Error : "+e.getLocalizedMessage());
      e.printStackTrace (System.err);

        // Sends a dialog box with the error
7.      ContainerHolder container = new ContainerHolder (BasicDlgs.informationDialog ("Server Error :"+e.getLocalizedMessage (), "OK"));
      container.writeContainer (response);
      } catch (Exception excp)
      {
      }
    }
  }


Explanations :

1. NAME is the parameter's name send by AppliWeb. It's value is the name typed by the user. This line of code gets it.

2. The servlet's response is an AppliWeb file. Here we give the name of the file to load.

3. ContainerHolder is a bean coming with AppliWeb which loads and holds dynamic web pages and allows access to their inner components.

Here we create a new one by passing it an AppliWebBean (which will de-serialize the file), and the file name.

4. We get the JLabel named "NAME" from the loaded page here, thanks to the functions of ContainerHolder. And we ask him to display the user's name.

5. Now we send the modified page to the client.

6. This code is runned in case of errors.

7. We send back a standard dialog box, were we display the error, to the client.

This way of doing is good only if the client and server are using the same jdk and swing release.
As the pages are sent as serialized beans, and this data format rely on the jdk used.

 


(c) 1999-2000 COLLIN Gérard