Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

getPathtranslated() returned null in tomcat 8 after dispather call  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I run same below program in tomcat 7 and tomcat 8 then I got null value in tomcat 8 but I got full translated path in tomcat 7.0.

http://localhost:8080/TestApp/request_info_example/apple+orange?param=zzz

web.xml entry
<servlet>
<servlet-name>TomcatRequestInfoExample</servlet-name>
<servlet-class>mypkg.RequestInfoExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TomcatRequestInfoExample</servlet-name>
<url-pattern>/request_info_example/*</url-pattern>
</servlet-mapping>

RequestInfoExample

String pathTraslated = request.getPathTranslated(); // Tomcat 8 returns D:\Tomcat-8.0.30\webapps\TestApp\apple+orange and tomcat 7 returns D:\Tomcat-7\webapps\TestApp\apple+orange
request.getRequestDispatcher("/home.jsp").forward(request, response);

but in home.jsp page

<%=request.getPathTranslated() %> //// Tomcat 8 returns null and tomcat 7 returns D:\Tomcat-7\webapps\TestApp\apple+orange

how to get request.getPathTranslated() value in tomcat 8 after Dispatcher call ?

I want to get result in home.jsp page same as tomcat 7 in tomcat 8 version.

it seems CGI PATH_TRANSLATED value is not getting in tomcat 8 if we call getRequestDispatcher().

Please help if someone know this in details and let me know what I have to do in tomcat 8 to achieve value same as tomcat 6 or 7.


 
Bartender
Posts: 20771
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getPathTranslated method is treacherous. It's designed to return the absolute filesystem path corresponding to the incoming URL.

But not all URLs correspond to filesystem locations. A webapp server, after all, is not a file server - files are only served in the event that the URL can be translated (based on the rules set in web.xml) to a filesystem location. Which itself is a fallback for when more specific ways of handling the URL have not been defined (for example, servlet resources or JSPs).

Furthermore, if you attempt to reference a static resource, but the webapp is in a WAR archive (that is, still zipped), then none of the resources have absolute filesystem paths, because the normal filesystems cannot access individual members of a ZIP, JAR or WAR file.

In either of the above cases, therefore, the getPathTranslated() method will return null.

For static resources, a better approach is to use the getResource or getResourceAsStream methods.
 
RajnishKushwaha Kushwaha
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,
Thank you for your interest to reply over on this topic however reply is not helpful as my question is different. this code is running in tomcat 7.0.28 but when I run same code in tomcat 8 then request.getPathTranslated() return null after dispatcher forward.

this line request.getPathTranslated() is being used in 500 files that we can not change in all the files.

I found the solution of this issue. I have created my own java file and inserted in tomcat jar file. Now it's working and no need to change on multiple lines and several codes.
 
I think I'll just lie down here for a second. And ponder this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!