FileDocCategorySizeDatePackage
WeatherHtmlKonaRevised.javaAPI DocExample4181Tue Jan 25 10:45:14 GMT 2000None

WeatherHtmlKonaRevised.java

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import weblogic.html.*;

class CurrentStyle extends StringElement {
  CurrentStyle(String val) {
    super(new StringElement(val)
          .asFontElement("+2")
          .asBoldElement()
          .asCenteredElement());
  }
}

class ExtendedTitleStyle extends StringElement {
  ExtendedTitleStyle(String val) {
    super(new StringElement(val)
          .asFontElement("+1")
          .asBoldElement()
          .asCenteredElement());
  }
}

class ExtendedDayStyle extends StringElement {
  ExtendedDayStyle(String val) {
    super(new StringElement(val)
          .asFontElement("+1"));
  }
}

class ExtendedTempStyle extends StringElement {
  ExtendedTempStyle(String val) {
    super(new StringElement(val)
          .asFontElement("+1")
          .asCenteredElement());
  }
}

class ImageStyle extends CenteredElement {
  ImageStyle(String src) {
    super(new ImageElement(src).setWidth(48).setHeight(35));
  }
}

public class WeatherHtmlKonaRevised extends HttpServlet {

  static final ImageStyle sunny = new ImageStyle("/images/sunny.gif");
  static final ImageStyle rainy = new ImageStyle("/images/rainy.gif");

  // Some static final variables to populate the page...
  // These would normally come from a database or 
  // maybe another servlet that retrieved it as POST data.

  static final int currentTemp = 70;
  static final ImageStyle currentImage = sunny;
  static final String[] forecastDay = { "Thursday", "Friday", "Saturday" };
  static final ImageStyle[] forecastImage = { sunny, sunny, rainy };
  static final int[] forecastHi = { 82, 82, 73 };
  static final int[] forecastLo = { 58, 65, 48 };

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    res.setContentType("text/html");

    // Get a new page
    ServletPage page = new ServletPage();
    
    // Set its title
    String title = "Sebastopol Weather Forecast";
    page.getHead().addElement(new TitleElement(title));

    // Get the body
    HtmlContainer body = page.getBody();

    // Make a table, and add it to the body (even before it's filled)
    TableElement tab = new TableElement()
                       .setCellPadding(0)
                       .setCellSpacing(0)
                       .setBorder(1)
                       .setWidth("60%");
    body.addElement(new CenteredElement(tab));

    // Create the first row
    HtmlElement conditions = new CurrentStyle("Current Conditions");
    HtmlElement image = currentImage;
    HtmlElement temp = new CurrentStyle(currentTemp + "°"); // degree symbol
    tab.addElement(new TableRowElement()
                   .addElement(new TableDataElement(conditions))
                   .addElement(new TableDataElement(image))
                   .addElement(new TableDataElement(temp)
                               .setColSpan(2)));

    // Create the second row
    HtmlElement extended = new ExtendedTitleStyle("Extended Forecast");
    HtmlElement hi = new ExtendedTitleStyle("Hi");
    HtmlElement lo = new ExtendedTitleStyle("Lo");
    tab.addElement(new TableRowElement()
                   .addElement(new TableDataElement(extended)
                               .setColSpan(2))
                   .addElement(new TableDataElement(hi))
                   .addElement(new TableDataElement(lo)));

    // Create the forecast rows
    for (int i = 0; i < forecastDay.length; i++) {
      HtmlElement day = new ExtendedDayStyle(" " + forecastDay[i]);
      HtmlElement daypic = forecastImage[i];
      HtmlElement dayhi = new ExtendedTempStyle("" + forecastHi[i]);
      HtmlElement daylo = new ExtendedTempStyle("" + forecastLo[i]);
      tab.addElement(new TableRowElement()
                   .addElement(new TableDataElement(day))
                   .addElement(new TableDataElement(daypic))
                   .addElement(new TableDataElement(dayhi))
                   .addElement(new TableDataElement(daylo)));
    }

    // Send the page to the response's output stream
    page.output(res.getOutputStream());
  }
}