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());