vim
vim

Reputation: 135

Tomcat 8 converts hyphens to %2D in the URL

How to prevent Tomcat from encoding hyphens (-) to %2D in URLs. This behaviour causes some session related issues when requests are made from Safari browser.

Deployed war file: my-app.war

Generated url: /my%2Dapp

Desired url: /my-app

Upvotes: 1

Views: 1385

Answers (3)

Michael-O
Michael-O

Reputation: 18405

If you are referring to the links generated by the Tomcat Manger: The issue you experience is rooted not in any Context configuration but in the HTMLManagerServlet. This servlet includes the following line:

 "<a href=\"" + URL_ENCODER.encode(contextPath + "/")

which is a custom encoder: org.apache.catalina.util.URLEncoder. The static instance employed in the code does not use the DEFAULT singleton which marks following chars as safe:

public static final URLEncoder DEFAULT = new URLEncoder();
static {
    DEFAULT.addSafeCharacter('~');
    DEFAULT.addSafeCharacter('-');
    DEFAULT.addSafeCharacter('_');
    DEFAULT.addSafeCharacter('.');
    DEFAULT.addSafeCharacter('*');
    DEFAULT.addSafeCharacter('/');
}

but rather does this:

static {
    URL_ENCODER = new URLEncoder();
    // '/' should not be encoded in context paths
    URL_ENCODER.addSafeCharacter('/');
}

So the outcome is that every single char except / is URL-encoded with UTF-8.

Upvotes: 3

borjab
borjab

Reputation: 11655

Just set the path in the context configuration:

<Context path="/myAppPath" docBase="h:/foo/mywar.war" reloadable="true" />

In this case you can use: http://domain/myAppPath/

Upvotes: 0

Vinh Vo
Vinh Vo

Reputation: 171

I think you shouldn't try to control this manually. Instead, you need to define your own context file under conf/Catalina/localhost/my-app.xml Then :

<?xml version="1.0"?>
<Context docBase="/some/path/to/my-app.war">
</Context>

You can play around with this until you get the correct behavior that you wanted.

Upvotes: 1

Related Questions