Escape characters in a JSP __jsp

Source: Internet
Author: User
Tags stringbuffer
In JSP writing often in the page using variable direct output (eg.<%=username%>), sometimes to display information, sometimes as a JS parameter provided, and sometimes used to assemble the URL. Because of the existence of an escape character of the HTML language itself, the direct output of the variable will cause the page to display abnormally, or JS syntax error, or URL error, this article discusses the analysis and solution of this problem, at the same time explore the struts framework for the solution of this problem, I hope that other similar problems can be inspired to solve.

Html:

&lt; Escape sequence for <
&gt; Escape sequence for >
&amp; Escape sequence for &
&quot; Escape sequence for "
Javascript:
Url:

Struts Solution:



Report:
public class Pageutil {/** * genarate escape sequance in HTML for special str * @see HTML 4.01 Specificati  On 5.3.2 Character Entity references */public static string escape4html (String str) {StringBuffer SB = 
        New StringBuffer (); 
            for (int i=0;i<str.length (); i++) {char c = str.charat (i);
            if (c = = ' ") sb.append (" &quot; ");
            else if (c = = ' < ') sb.append ("&lt;");
            else if (c = = ' > ') sb.append ("&gt;");
        else  Sb.append (c); 
    return sb.tostring ();  }/** * Genarate escape sequance in JavaScript for special str * @see JavaScript Language 1.1 specification  2.7.5 Escape sequences for String literals/public static String Escape4js (String str) {StringBuffer 
        SB = new StringBuffer (); for (int i=0;i<str.length (); i++) {char c = Str.chaRAt (i);
            if (c = = '/') sb.append ("///'");
            else if (c = = ' ") sb.append ("///");
        else  Sb.append (c); 
    return sb.tostring ();
 public static void Main (string[] args) {}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.