Prints a conversion table of miles per gallon to kilometers per liter : HTML Output « Servlets « Java

Prints a conversion table of miles per gallon to kilometers per liter


*  Copyright (c) 2002 by Phil Hanna
*  All rights reserved.
*  You may study, use, modify, and distribute this
*  software for any purpose provided that this
*  copyright notice appears in all copies.
*  This software is provided without warranty
*  either expressed or implied.
package com.jspcr.servlets;

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

* Prints a conversion table of miles per gallon
* to kilometers per liter
public class K2MServlet extends HttpServlet
   * Numeric format used to display temperatures
   private static final DecimalFormat FMT
      = new DecimalFormat("#0.00");

   * Factor to convert from km/l to mi/gal
   private static final double CONVERSION_FACTOR = 2.352145;

   * Handles a GET request
   public void doGet(
         HttpServletRequest request,
         HttpServletResponse response)
      throws ServletException, IOException
      // Set up for creating HTML output

      PrintWriter out = response.getWriter();

      // Generate heading

         ( "<html>"
         + "<head>"
         + "<title>Fuel Efficiency Conversion Chart</title>"
         + "</head>"
         + "<body>"
         + "<center>"
         + "<h1>Fuel Efficiency Conversion Chart</h1>"
         + "<table border='1' cellpadding='3' cellspacing='0'>"
         + "<tr>"
         + "<th>Kilometers per Liter</th>"
         + "<th>Miles per Gallon</th>"
         + "</tr>"

      // Generate table

      for (double kmpl = 5; kmpl <= 20; kmpl += 1.0) {
         double mpg = kmpl * CONVERSION_FACTOR;
            ( "<tr>"
            + "<td align='right'>" + FMT.format(kmpl) + "</td>"
            + "<td align='right'>" + FMT.format(mpg) + "</td>"
            + "</tr>"

      // Generate footer

         ( "</table>"
         + "</center>"
         + "</body>"
         + "</html>"


Prints 92 k)

Related examples in the same category

1.Servlet Output HTML Demo
2.Servlet Display Static HTML
3.Servlet: Print Table
4.Html utilities
5.Html Parse Servlet
6.Escape and unescape string
7.Escapes newlines, tabs, backslashes, and quotes in the specified string
8.Web Calendar
9.HTML Helper
10.Escape HTML
11.Convert HTML to text
12.Text To HTML
13.Unescape HTML
14.Java object representations of the HTML table structure
15.Entity Decoder
16.Format a color to HTML RGB color format (e.g. #FF0000 for
17.Definitions of HTML character entities and conversions between unicode characters and HTML character entities
18.Encode special characters and do formatting for HTML output
19.HTML color names
20.Utility methods for dealing with HTML
21.Filter the specified message string for characters that are sensitive in HTML
22.A collection of all character entites defined in the HTML4 standard.
23.Decode an HTML color string like '#F567BA;' into a Color
24.Normalize Post Data
25.Get HTML Color String from Java Color object
26.HTML Decoder
27.HTML Parser
28.HTML color and Java Color
29.HTML form Utilites
30.Html Dimensions
31.break Lines with HTML
32.insert HTML block dynamically
33.Convert an integer to an HTML RGB value
34.Convert to HTML string