import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import weblogic.html.*; public class WeatherHtmlKona 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"); // 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 StringElement("Current Conditions") .asFontElement("+2") .asBoldElement() .asCenteredElement(); HtmlElement image = new ImageElement(currentImage) .setWidth(48) .setHeight(35) .asCenteredElement(); HtmlElement temp = new StringElement(currentTemp + "°") .asFontElement("+2") .asBoldElement() .asCenteredElement(); 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 StringElement("Extended Forecast") .asFontElement("+1") .asBoldElement() .asCenteredElement(); HtmlElement hi = new StringElement("Hi") .asFontElement("+1") .asBoldElement() .asCenteredElement(); HtmlElement lo = new StringElement("Lo") .asFontElement("+1") .asBoldElement() .asCenteredElement(); 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 StringElement(" " + forecastDay[i]) .asFontElement("+1"); HtmlElement daypic = new ImageElement(forecastImage[i]) .setWidth(48) .setHeight(35) .asCenteredElement(); HtmlElement dayhi = new StringElement("" + forecastHi[i]) .asFontElement("+1") .asCenteredElement(); HtmlElement daylo = new StringElement("" + forecastLo[i]) .asFontElement("+1") .asCenteredElement(); 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()); } }