Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple error due to use of double quotes in a jsp file

I have the following line of code in a JSP File in my web app that is giving an error:

<jsp:setProperty name="db" property="userName" value="<%=request.getParameter("userName")%>"/> 

The error message that I get is:

org.apache.jasper.JasperException: /loginbean.jsp(6,59) Attribute value request.getParameter("userName") is quoted with " which must be escaped when used within the value

What I read on some sites is that characters like ' (single quote) or " (double quote) need to be prefixed with an escape sequence \ (backslash) if they are to be used.

However, when I try and prefix the double quotes (around the word userName) with backslash, I immediately get the following error- "Illegal Character \92- Unclosed String Literal"

How do I resolve this problem?

like image 814
Arvind Avatar asked Jun 28 '11 03:06

Arvind


People also ask

How do you handle double quotes?

If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.

How do you replace double quotes in Java?

replaceAll("\"","\\\""); //details. replaceAll("\"","&quote;"); details.

What are double quotes used for in Java?

A string literal is bracketed by either single quotes ( ' ) or double quotes ( " ). When single quotes bracket a string literal, the value of the literal is the value within the quotes. When double quotes are used, any references to variables or expressions within the quotes are interpolated.

How do you ignore a double quote in a string?

This method replaces all parts of the String that match a given regular expression. Using replaceAll, we can remove all occurrences of double quotes by replacing them with empty strings: String result = input. replaceAll("\"", "");


2 Answers

You should use single quotes on the value parameter, ie:

value='<%=request.getParameter("userName")%>' 

or set the org.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING parameter to false as described here:

http://blogs.sourceallies.com/2009/10/strict-quote-escaping-in-tomcat/

like image 179
ryanprayogo Avatar answered Oct 03 '22 10:10

ryanprayogo


If you are using Tomcat 8.5+, the property org.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING=false will not be acknowledged.

I was able to set the property successfully in {TOMCAT_ROOT}/conf/web.xml by adding the following within the <servlet> block:

<init-param>     <param-name>strictQuoteEscaping</param-name>     <param-value>false</param-value> </init-param> 
like image 21
Dump Cake Avatar answered Oct 03 '22 10:10

Dump Cake