Pages

Thursday 21 March 2013

Servlet: HTML filtering

I took this instance from "Core Servlets and JavaServer Pages" book by Marty Hall and Larry Brown.
This is my text book at the moment.
Here is some code:
The main problem is html-tags.. If you try to print out "a<b" (a less than b) - you will not see any text after < symbol. But the method filter() can replace "<" symbol with "&lt;"  (predefined letters combination):



/** <b - as a html tag:
     if (a<b) {
        doThis();
     } else {
        doThat();
     }
     */
    public static String filter(String input) {
        StringBuilder filtered = new StringBuilder(input.length());
        char c;
        for(int i=0; i<input.length(); i++) {
            c = input.charAt(i);
            if (c == '<') {
                filtered.append("&lt;");
            } else if (c == '>') {
                filtered.append("&gt;");
            } else if (c == '"') {
                filtered.append("&quot;");
            } else if (c == '&') {
                filtered.append("&amp;");
            } else {
                filtered.append(c);
            }
        }
        return(filtered.toString());
    } 


JSP code:
<%-- 
    Document   : index
    Created on : 20-Mar-2013, 18:55:39
    Author     : Alex
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>HTML Converter</title>
    </head>
    <body>
        <div style="width:500px;float:left;display:inline-block;
             background:#fbdce1;border:5px solid #fa6e6e;padding:10px;">
            <form name="Frm1" action="convert" method="POST">
                <TEXTAREA NAME="txt" ROWS=10 COLS=50 WRAP="SOFT" 
                          style ="max-width:490px;"></TEXTAREA><BR />
                <input type="submit" value="Convert" name="SubmitBtn" />
            </form>
        </div>
    </body>
</html>
 

Here is a servlet code (processRequest() method of a servlet) where everything is happening. This code wos converted by filter() methid:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            String txt = request.getParameter("txt");
            txt = myServUtil.replaceIfMissing(txt, "<NOT FOUND ANY TEXT>");
            txt = myServUtil.filter(txt);
            
            String title = "HTML filtering result";
            String body = "<div style=\"width:500px;float:left;display:inline-block;\n" +
                    "background:#fbdce1;border:5px solid #fa6e6e;padding:10px;\">" +
                    "<form name=\"Frm1\" action=\"convert\" method=\"POST\">" +
                    "<TEXTAREA NAME=\"txt\" ROWS=10 COLS=50 WRAP=\"SOFT\" 
                       style =\"max-width:490px;\">\n\n"+ txt + "\n</TEXTAREA><br />" +
                    "</form>" + "<br />" +"Try again: " +
                    "<input type=\"submit\" value=\"Convert\" name=\"SubmitBtn\" />" +
                    "</div>";
            String html = myServUtil.getHTML(title, body);
            out.println(html);
        } catch(Exception ex) {
            ex.printStackTrace(out);
        }
        finally {            
            out.close();
        }
    }

If you try to convert ("filter") an empty text you receive such a output:

<!DOCTYPE html> 
<html> 
<head> 
<title>HTML filtering result</title> 
</head> 
<body> 
<div style="width:500px;float:left;display:inline-block;
             background:#fbdce1;border:5px solid #fa6e6e;padding:10px;">
<form name="Frm1" action="convert" method="POST">
<TEXTAREA NAME="txt" ROWS=10 COLS=50 WRAP="SOFT" style ="max-width:490px;">

&lt;NOT FOUND ANY TEXT&gt;
</TEXTAREA><br /></form><br />Try again: 
<input type="submit" value="Convert" name="SubmitBtn" /></div>
</body> 
</html>

P.s. I used CSS style only for decoration.

No comments:

Post a Comment