user3147325
user3147325

Reputation: 63

HttpservletRequest path info is not getting set properly

I set the Path INFO for my HttpservletRequest as below.

request.setAttribute("javax.servlet.include.path_info", pathInfo);

After this statement i tried to get the pathinfo , but that is returning null.

String info = request.getPathInfo();

info is null here.

Am I setting the path correctly?

Upvotes: 0

Views: 1916

Answers (2)

avijendr
avijendr

Reputation: 4153

The second part is wrong. If you set an attribute to the servlet request, you can get it only via the getAttribute method.

so if you set the value using:

request.setAttribute("javax.servlet.include.path_info", pathInfo);

The you'd get that value back using:

request.getAttribute("javax.servlet.include.path_info");

Now request.getPathInfo() gives the extra path information after the URI. In your case it would be null, because there would be nothing after the URI:

E.G - if you have a url = http://someHost.com/servletName?id=1234&name=fred

request.getPathInfo() would return id=1234*name=fred

Upvotes: 1

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298838

It doesn't work that way. HttpServletRequest objects are read-only, apart from the attributes. What you can do though is replace the request object with a wrapped one that returns the Path info you want. Usually you would do that in a Filter and wrap the request in a HttpServletRequestWrapper.

Upvotes: 2

Related Questions