FileDocCategorySizeDatePackage
WeatherHtml.javaAPI DocExample3223Tue Jan 25 10:45:14 GMT 2000None

WeatherHtml.java

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

public class WeatherHtml extends HttpServlet {

  // 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 String currentImage = "/images/rainy.gif";
  static final String[] forecastDay = { "Thursday",
                                        "Friday",
                                        "Saturday" };
  static final String[] forecastImage = { "/images/sunny.gif", 
                                          "/images/sunny.gif",
                                          "/images/rainy.gif" };
  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");
    PrintWriter out = res.getWriter();

    // Set its title
    String title = "Sebastopol Weather Forecast";
    out.println("<HTML>");
    out.println("<HEAD>");
    out.println("<TITLE>" + title + "</TITLE>");
    out.println("</HEAD>");

    // Start on the body
    out.println("<BODY>");

    // Make a centered table
    out.println("<CENTER>");
    out.println("<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0 WIDTH=70%>");

    // First row
    out.println("<TR>");
    out.println("<TD><CENTER><B>");
    out.println("<FONT SIZE=+2>Current Conditions</FONT>");
    out.println("</B></CENTER></TD>");

    out.println("<TD><CENTEr>");
    out.println("<IMG WIDTH=48 HEIGHT=35 SRC=\"" + currentImage + "\">");
    out.println("</CENTER></TD>");

    out.println("<TD COLSPAN=2><CENTER><B><FONT SIZE=+2>");
    out.println(currentTemp + "°");
    out.println("</FONT></B></CENTER></TD>");
    out.println("</TR>");

    // Second row
    out.println("<TR>");
    out.println("<TD COLSPAN=2><CENTER><B><FONT SIZE=+1>");
    out.println("Extended Forecast");
    out.println("</FONT></B></CENTER></TD>");

    out.println("<TD><CENTER><B><FONT SIZE=+1>");
    out.println("Hi");
    out.println("</FONT></B></CENTER></TD>");

    out.println("<TD><CENTER><B><FONT SIZE=+1>");
    out.println("Lo");
    out.println("</FONT></B></CENTER></TD>");
    out.println("</TR>");

    // Daily forecast rows
    for (int i = 0; i < forecastDay.length; i++) {
      out.println("<TR>");
      out.println("<TD> <FONT SIZE=+1>");
      out.println(forecastDay[i]);
      out.println("</FONT></TD>");
      out.println("<TD><CENTER>");
      out.println("<IMG WIDTH=48 HEIGHT=35 SRC=\"" + forecastImage[i] + "\">");
      out.println("</CENTER></TD>");
      out.println("<TD><CENTER><FONT SIZE=+1>");
      out.println(forecastHi[i]);
      out.println("</FONT></CENTER></TD>");
      out.println("<TD><CENTER><FONT SIZE=+1>");
      out.println(forecastLo[i]);
      out.println("</FONT></CENTER></TD>");
      out.println("</TR>");
    }

    // Close the still-open tags
    out.println("</TABLE>");
    out.println("</CENTER>");
    out.println("</BODY></HTML>");
  }
}