When i need to escape Html string?

前端 未结 4 1906
孤独总比滥情好
孤独总比滥情好 2021-02-08 02:36

In my legacy project i can see the usage of escapeHtml before string is sent to browser.

StringEscapeUtils.escapeHtml(stringBody);

I know from

4条回答
  •  旧时难觅i
    2021-02-08 03:05

    From my experience, all of the strings should be escaped from Html before being displayed on the page. Our current project is about managing all the Organization Units from the Active Directory, and these units could contain any special character (including Html Character). When displaying on the page, you could end up with the following code to show a record called User

     <%=request.getAttribute("Name");%> 
    

    after the page is rendered, it will become

     User  
    

    Which actually appears as User hyperlink on the page.

    However, if you escape the Html value before sending to the page

    request.setAttribute("Name", StringEscapeUtils.escapeHtml("User "));
    

    after the page is rendered, it will become

      User <Marketing> 
    

    which appear correctly on the JSP page

    Shortly, you use escaping Html characters to prevent the special input. If the input contains the Html Character, your page will appear wrong during rendering

提交回复
热议问题