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>
user1315305
  • 1,329
  • 2
  • 11
  • 20
  • 1
    and if you use single quote (use the `replace(char,char)`) – ratchet freak Jun 09 '12 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? – Nickoli Roussakov Jun 09 '12 at 23:20
  • @ratchet freak, I just checked the API. I think you're right here. You should put your comment as an answer. – Nickoli Roussakov Jun 09 '12 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 '12 at 00:22
  • You should look up StringEscapeUtils, and use the XML Encoder in there. http://stackoverflow.com/questions/439298/best-way-to-encode-text-data-for-xml-in-java – billjamesdev Jun 10 '12 at 02:32
  • @BalusC - I need it to access an XML file external to my .war file, located in tomcat/webapps to perform XSL transformation. Could you suggest some better way to do it? – user1315305 Jun 10 '12 at 06:22
  • What's the next bit of XML? I'm wondering whether you need to do anything about the backslashes at all. You usually don't. – user207421 Jun 10 '12 at 07:46
  • @EJP - I updated my first post with the code where I'm using this variable – user1315305 Jun 10 '12 at 08:55

1 Answers1

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("\\\\", "/").

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 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 '12 at 15:10