If you want to get the path information appended to your script URL, you can use the PATH_INFO CGI variable. Consider the following CGI script:

#!/bin/bash

echo "Content-Type: text/plain"
echo
echo $PATH_INFO

all this script does it output the PATH_INFO. This script is available at http://swamp.homelinux.net/cgi-bin/pathinfo. If you click on the link, you will see an empty page in your browser. This is because PATH_INFO has not been set because there is no extra path information on the URL. However, if you go to http://swamp.homelinux.net/cgi-bin/pathinfo/foo you will see “/foo” in your browser. Go ahead and play with the URL to see how PATH_INFO works.

The following is an example how to print out the path info in PHP:

<?php
echo $_SERVER['PATH_INFO'];
?>

If you’re using Java, you can get the path info from HttpServletResponse.getPathInfo(). Consider the following servlet:

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/plain");
		resp.getWriter().print(req.getPathInfo());
	}
}

You can map the servlet in your web.xml using the following snippet:

<servlet>
	<servlet-name>Test Servlet</servlet-name>
	<servlet-class>TestServlet</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>Test Servlet</servlet-name>
	<url-pattern>/test/*</url-pattern>
</servlet-mapping>

You must have the * trailing your URL pattern or the servlet container won’t pick up the extra path info and pass it to your servlet.