6

This is a special case where I am stuck dealing with an "enterprise" webapp. The enterprise app is calling request.getContext() and comparing it to another string.

I see I can get the servlet context path using getServletContext.getContextPath(). Is it possible to set the request context path from inside a servlet?

1
  • 2
    I assume that you're talking about HttpServletRequest#getContextPath() since there's no such method as HttpServletRequest#getContext().
    – BalusC
    Apr 5, 2011 at 17:20

2 Answers 2

5

You cannot set it, but you can change its outcome with help of HttpServletRequestWrapper.

public class MyRequest extends HttpServletRequestWrapper {

    public MyRequest(HttpServletRequest request) {
        super(request);
    }

    @Override    
    public String getContextPath() {
        return "a different context path"; // TODO: implement accordingly.
    }

}

Create a Filter which does basically the following in doFilter() method and map it as an early filter on URL pattern of interest.

chain.doFilter(new MyRequest((HttpServletRequest) request), response);
0
0

No, It gets set when you deploy your app. You can't change it once it is deployed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.