HTML form Utilites : HTML Output « Servlets « Java






HTML form Utilites

 


//Revised from open ejb
public class HtmlUtilities {

    public static final String ANCHOR_NAME_TYPE = "name";

    public static final String ANCHOR_HREF_TYPE = "href";

    //we don't want anyone creating new instances of this class
    private HtmlUtilities() {
    }

    public static String createAnchor(String value, String display, String type) {
        //our type must be one of these two
        if (!(ANCHOR_HREF_TYPE.equals(type) || ANCHOR_NAME_TYPE.equals(type))) {
            throw new IllegalArgumentException("The type argument must be either \"name\" or \"href\"");
        }

        return new StringBuffer(100)
                .append("<a ")
                .append(type)
                .append("=\"")
                .append(value)
                .append("\">")
                .append(display)
                .append("</a>")
                .toString();
    }

    public static String createSelectFormField(String name, String onChange) {
        StringBuffer temp = new StringBuffer(60).append("<select name=\"").append(name);

        if (onChange != null) {
            temp.append("\" onChange=\"").append(onChange);
        }

        return temp.append("\">").toString();
    }

    public static String createSelectOption(String value, String display, boolean selected) {
        StringBuffer temp = new StringBuffer(65).append("<option value=\"").append(value).append("\"");

        if (selected) {
            temp.append(" selected");
        }

        return temp.append(">").append(display).append("</option>").toString();
    }

    public static String createTextFormField(String name, String value, int size, int maxLength) {
        return createInputFormField("text", name, value, size, maxLength, null, null, null, null, false, false, false);
    }

    public static String createFileFormField(String name, String value, int size) {
        return createInputFormField("file", name, value, size, 0, null, null, null, null, false, false, false);
    }

    public static String createHiddenFormField(String name, String value) {
        return createInputFormField("hidden", name, value, 0, 0, null, null, null, null, false, false, false);
    }

    public static String createSubmitFormButton(String name, String value) {
        return createInputFormField("submit", name, value, 0, 0, null, null, null, null, false, false, false);
    }

    public static String createInputFormField(
            String type,
            String name,
            String value,
            int size,
            int maxLength,
            String onFocus,
            String onBlur,
            String onChange,
            String onClick,
            boolean checked,
            boolean disabled,
            boolean readOnly) {

        StringBuffer temp = new StringBuffer(150)
                .append("<input type=\"")
                .append(type)
                .append("\" name=\"")
                .append(name)
                .append("\" value=\"")
                .append(value)
                .append("\"");

        if (size > 0) {
            temp.append(" size=\"").append(size).append("\"");
        }
        if (maxLength > 0) {
            temp.append(" maxlength=\"").append(maxLength).append("\"");
        }
        if (onFocus != null) {
            temp.append(" onfocus=\"").append(onFocus).append("\"");
        }
        if (onBlur != null) {
            temp.append(" onblur=\"").append(onBlur).append("\"");
        }
        if (onChange != null) {
            temp.append(" onchange=\"").append(onChange).append("\"");
        }
        if (onClick != null) {
            temp.append(" onclick=\"").append(onClick).append("\"");
        }
        if (checked) {
            temp.append(" checked");
        }
        if (disabled) {
            temp.append(" disabled");
        }
        if (readOnly) {
            temp.append(" readonly");
        }

        return temp.append(">").toString();
    }

    public static String createTextArea(
            String name,
            String content,
            int rows,
            int columns,
            String onFocus,
            String onBlur,
            String onChange) {
        StringBuffer temp = new StringBuffer(50);
        temp.append("<textarea name=\"").append(name).append("\" rows=\"").append(rows).append("\" cols=\"").append(
                columns).append(
                "\"");

        if (onFocus != null) {
            temp.append(" onfocus=\"").append(onFocus).append("\"");
        }
        if (onBlur != null) {
            temp.append(" onblur=\"").append(onBlur).append("\"");
        }
        if (onChange != null) {
            temp.append(" onchange=\"").append(onChange).append("\"");
        }

        return temp.append(">").append(content).append("</textarea>").toString();
    }
}

   
  








Related examples in the same category

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