简体   繁体   English

防止XSS并使用c:out jsp标记

[英]Protecting from XSS and using c:out jsp tag

A ${book.description} variable contains text with "\\n" character incide, so I replace it to html tag to preserve new line (new paragrapf line) in jsp output. $ {book.description}变量包含带有“ \\ n”字符斜率的文本,因此我将其替换为html标记以在jsp输出中保留新行(新paragrapf行)。 But in this case there is a danger of execution of the XSS code. 但是在这种情况下,存在执行XSS代码的危险。 Is there a way do it using c:out jsp tag and preserv new line in text? 有没有办法使用c:out jsp标记并在文本中保留新行? If I use c:out for output then all < br/ >replaced to & lt; 如果我将c:out用于输出,那么所有<br />都将替换为&lt; br / & gt; br /&gt; and there is no new line. 而且没有换行。

<c:set var="newline" value="<%= \"\n\" %>" />           
<p>${fn:replace(book.description, newline, "<br />")}</p>

You might interested in other functions of JSTL Functions . 您可能对JSTL Functions的其他功能感兴趣。

Have a look at ${fn:escapeXml(text)} that escapes XML characters in a string. 看一下${fn:escapeXml(text)} ,它会转义字符串中的XML字符。 Here is the Sample . 这是样本


Have a look at 看一下

<c:out value="value" [escapeXml="{true|false}"] [default="defaultValue"] />

Read more tutorial on Core Tag Library 阅读有关核心标签库的更多教程


EDIT 编辑

Solution 1 解决方案1

<c:set var="newline" value="\n" />
<c:set var="text" value="abc\nlmn\nxyz" /> 
${ fn:replace(text, newline,'</br>')}

Solution 2 解决方案2

<c:set var="newline" value="\n" />
<c:set var="text" value="abc\nlmn\nxyz" /> 
<c:out value="${ fn:replace(text, newline,'</br>')}" escapeXml="false"/>

output: 输出:

abc
lmn
xyz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM