Java servlet example - HttpServletRequest, getPathInfo, and getRequestURI

Working on tag support for this blog, I've cut a little sample code out of a Java servlet, and I'm showing it below. I was looking at the HttpServletRequest, and was curious about the difference between request.getPathInfo(), request.getPathTranslated(), and request.getRequestURI(), so I created this demo code. Here's the example code from the servlet:

String pathInfo = request.getPathInfo();
String pathTrans = request.getPathTranslated();
String uri = request.getRequestURI();

System.err.println("pathInfo:  " + pathInfo);
System.err.println("pathTrans: " + pathTrans);
System.err.println("uri:       " + uri);

And here's the sample output from the servlet, running under Tomcat on my MacBook Pro:

pathInfo:  /java+mac+foo
pathTrans: /Users/al/tomcat-4.1.31/webapps/blog/tag/java+mac+foo
uri:       /blog/tag/java+mac+foo

I forgot to mention, the URL I hit to generate this output was http://localhost:8080/blog/tag/java+mac+foo

 

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Permalink

Is the pathTrans output correct? I think it should be /Users/al/tomcat-4.1.31/webapps/blog/tag/java+mac+foo. Anyway this is a great example, thx :)

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.