What is the simplest way to use a variable in the URL in servlets.
Eg. http://somesite.com/MyServlet/[ID]
What is the simplest way to use a variable in the URL in servlets.
Eg. http://somesite.com/MyServlet/[ID]
That's called path info. You can use HttpServletRequest#getPathInfo()
to grab it.
String pathInfo = request.getPathInfo(); // "/[ID]"
This however includes the leading slash. You may want to substring it away as follows:
String pathInfo = request.getPathInfo().substring(1); // "[ID]"
This assumes that your servlet is mapped on an url-pattern
of /MyServlet/*
.