import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class SpecialCharacterServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("
");
out.println("HTML Tutorial -- Changing Line");
out.println("");
out.println("");
out.println(encodeHtmlTag("In HTML, you use
to change line."));
out.println("");
out.println("");
}
/**
* Encode an HTML tag so it will be displayed
* as it is on the browser.
* Particularly, this method searches the
* passed in String and replace every occurrence
* of the following character:
* '<' with "<"
* '>' with ">"
* '&' with "&"
* //'"' with """
* ' ' with " "
*/
public static String encodeHtmlTag(String tag) {
if (tag==null)
return null;
int length = tag.length();
StringBuffer encodedTag = new StringBuffer(2 * length);
for (int i=0; i')
encodedTag.append(">");
else if (c=='&')
encodedTag.append("&");
else if (c=='"')
encodedTag.append("""); //when trying to output text as tag's value as in
// values="???".
else if (c==' ')
encodedTag.append(" ");
else
encodedTag.append(c);
}
return encodedTag.toString();
}
}