which must be escaped when used within the value 問

視頻課堂:https://edu.csdn.net/course/play/8222

訪問JSP時,報錯:Attribute value is quoted with " which must be escaped when used within the value 。相信很多人都遇到過,以下結合本人查資料與親自實踐得到的結論。

有不對的地方還請指教。

 

1):錯誤常發生的位置 value="<%=""+request.getAttribute("name")%>"  

2):錯誤產生的原因,是因爲tomcat版本升級後(6.0以後),對雙引號的處理機制引起的,如果出現雙引號包含雙引號的情況,就可能會出現這個錯誤。

3):解決辦法有好多種,大多數網上的意見是改爲value=‘<%=""+request.getAttribute("name")%>’ 這樣確實可以解決這個報錯,可能無法完全解決問題,比如項目中有很多地方以前都用的是雙引號,修改起來會非常麻煩,還容易出錯。

         查看tomact文檔有一段話這樣描述的“Iffalsethe requirements for escaping quotes in JSP attributes will be relaxed so that an unescaped quote will not cause an error.

If not specified, the specification compliant default oftruewill be used.”    這個描述針對的是tomact的org.apache.jasper.compiler. Parser.STRICT_QUOTE_ESCAPING屬性。

       如此一來,我們知道只要在tomcat啓動時將這個屬性設爲false就可以解決這個問題。

       那麼怎麼將這個屬性設置到tomcat的system屬性中呢,有至少兩個辦法:

      一種是利用tomcat啓動的執行腳本,在執行過程中調用setenv.sh,在其中設置環境變量,如:JAVA_OPTS=%JAVA_OPTS%: -Dorg.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING=false

     另一種是找到conf/catalina.properties文件,在最後添加org.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING=false 即可。

方法 3:使用轉義字符("\")

1<jsp:include page="split_page.jsp">
2    <jsp:param name="jspUrl" value="<%=jspUrl%>" />
3    <jsp:param name="category" value="<%=equest.getParameter(\"category\")%>" />
4    <jsp:param name="searchFlag" value="T" />
5</jsp:include>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章