| Summary: | ServletContextAdapter.getContextPath returns incorrect path | ||
|---|---|---|---|
| Product: | [Eclipse Project] Equinox | Reporter: | Scott Lewis <slewis> |
| Component: | Server-Side | Assignee: | Simon Kaegi <simon_kaegi> |
| Status: | RESOLVED FIXED | QA Contact: | |
| Severity: | major | ||
| Priority: | P3 | CC: | simon_kaegi |
| Version: | 3.7 | ||
| Target Milestone: | 3.7 M7 | ||
| Hardware: | PC | ||
| OS: | Windows XP | ||
| Whiteboard: | |||
Thanks Scott, We can put a guard but maybe a bit deeper. proxyContext.getServletPath() should probably be returning the empty string instead of null and need to figure out why it isn't. (In reply to comment #1) > Thanks Scott, > > We can put a guard but maybe a bit deeper. proxyContext.getServletPath() should > probably be returning the empty string instead of null and need to figure out > why it isn't. Sounds good...thanks. Looking a bit deeper I can see what's going on. The reason is that until the first "service" call the proxy servlet does not know what the servlet path is. With that said we really want to avoid returning null so if not set we now return "" instead of a null value for servlet path. This fix will correctly handle the typical /* mapping case but more complex mappings will continue to run into this problem and should instead either defer using contextpath methods to service calls by using the http.registry lazy init servlet activation (or do something similar themselves). The other perhaps more intrusive alternative is to altogether avoid getServletPath in servlet.init calls but I understand that might not be viable across the board. |
In the this method: org.eclipse.equinox.http.servlet.internal.ServletContextAdaptor.getContextPath() is currently this code: // Added in Servlet 2.5 public String getContextPath() { try { Method getContextPathMethod = servletContext.getClass().getMethod("getContextPath", null); //$NON-NLS-1$ return (String) getContextPathMethod.invoke(servletContext, null) + proxyContext.getServletPath(); } catch (Exception e) { // ignore } return null; } Under some conditions (i.e. when using the servletbridge, and calling servletContext.getContextPath() from within the init(ServletConfig config) method of a Servlet), the line with the following: return (String) getContextPathMethod.invoke(servletContext, null) + proxyContext.getServletPath(); Can return a bogus context path...because the reflective call to getContextPathMethod.invoke(servletContext,null) will return a non-null path...e.g. "/foo" and the call to proxyContext.getServletPath() will return *null*...meaning that the overall result will be (e.g.): contextPath = "/foonull" It seems that the string concat should probably be guarded by a null check for the value returned from proxyContext.getServletPath().