0

in my application I need to prepare a path for XML file inside JSP page. I'm doing someting like this:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml"  prefix="x" %>
<!DOCTYPE html>
<c:set var="abs_path" value='<%= getServletContext().getRealPath("").replace(" ", "%20").replace("\\", "/") %>' />

But there is a problem, I get the followind exception:

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 6 in the jsp file: /users.jsp
String literal is not properly closed by a double-quote
3: <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
4: <%@ taglib uri="http://java.sun.com/jsp/jstl/xml"  prefix="x" %>
5: <!DOCTYPE html>
6: <c:set var="abs_path" value='<%= getServletContext().getRealPath("").replace(" ", "%20").replace("\\", "/") %>' />

Apparently it's about this part: .replace("\\", "/") When I delete it, I don't get this exception.

What's that about? I will be very grateful for any clue.

EDIT:

I use this variable in the following way:

                <c:import url="file:/${abs_path}/MyProject/xml/users.xml" var="inputDoc" charEncoding="UTF-8" />

                <c:import url="xsl/users_list.xsl"
                        var="stylesheet" charEncoding="UTF-8" />

                <x:transform 
                     xml  = "${inputDoc}"
                     xslt = "${stylesheet}">
                </x:transform>
8
  • 1
    and if you use single quote (use the replace(char,char)) Jun 9, 2012 at 23:16
  • Have you tried putting the value attribute in double quotes instead of single quotes so it's consistent with the var attribute? Jun 9, 2012 at 23:20
  • @ratchet freak, I just checked the API. I think you're right here. You should put your comment as an answer. Jun 9, 2012 at 23:26
  • 1
    Unrelated to the concrete problem, why exactly do you need that information in a JSP? Whatever functional requirement you thought to solve with this approach, it has definitely to be solved differently.
    – BalusC
    Jun 10, 2012 at 0:22
  • You should look up StringEscapeUtils, and use the XML Encoder in there. stackoverflow.com/questions/439298/… Jun 10, 2012 at 2:32

1 Answer 1

2

It is not about "\\" being a regular expression ... because that wouldn't result in a JSP compilation error. (And besides, the argument of String.replace(String) isn't interpreted as a regex.)

However, the compilation error does seem to be saying that you need double escaping, and I think that the reason is that the JSP syntax is "consuming" one level of escaping itself ... in this context.

This is from the JSP 2.1 spec:

JSP.1.6 Quoting and Escape Conventions

...

Quoting in Attributes

Quotation is done consistently regardless of whether the attribute value is a literal or a request-time attribute expression. Quoting can be used in attribute values regardless of whether they are delimited using single or double quotes. It is only required as described below.

  • A is quoted as \’. This is required within a single quote-delimited attribute value.
  • A is quoted as \”. This is required within a double quote-delimited attribute value.
  • A \ is quoted as \\
  • Only when the EL is enabled for a page (see Section JSP.3.3.2, “Deactivating EL Evaluation”), a literal $ can be quoted by \$. Similarly, a literal # can be quoted by \#. This is not required but is useful for quoting EL expressions.
  • A %> is quoted as %\>
  • A <% is quoted as <\%
  • The entities &apos; and &quot; are available to describe single and double quotes.

Anyway, try writing the offending code fragment as replace("\\\\", "/").

1
  • The // works because URL normalization rules treat // as meaning the same thing as / in path. You didn't need to double the forward-slash characters, but it is harmless in this use-case.
    – Stephen C
    Jun 10, 2012 at 15:10

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.