检查 getServletPath 和 getPathInfo 方法是否有行为更改

缺省 servlet 映射仅包含 / 字符的映射。在竞争应用程序服务器中,对缺省 servlet 映射的 javax.servlet.http.HttpServletRequest.getServletPath 方法的调用会返回将请求 URI 减去上下文路径所得到的值,并且对 javax.servlet.http.HttpServletRequest.getPathInfo 方法的调用会返回 null。在 WebSphere Traditional 中,对缺省 servlet 映射的 getServletPath 的调用会返回空字符串,而对 getPathInfo 方法的调用会返回 / 字符。 同样,如果要迁移到 Liberty 并使用 Servlet 3.0 或 3.1 功能部件实现,那么对缺省 servlet 映射的 getServletPath 的调用会返回空字符串,而对 getPathInfo 方法的调用会返回 / 字符。

例如,请考虑以下代码:

@WebServlet("/")
public class TestServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) {
System.out.println("getServletPath: " + req.getServletPath());
System.out.println("getPathInfo: " + req.getPathInfo());
}
}

在竞争应用程序服务器中,此代码将产生以下输出:

getServletPath: /some/path/to/servlet
getPathInfo: null

但是,在 WebSphere Traditional 与具有 Servlet 3.0 或 3.1 的 Liberty 中,此代码将产生以下输出:

getServletPath:
getPathInfo: /

如果要迁移到 WebSphere Liberty 并使用 Servlet 3.0 或 3.1 实现,请将 <webContainer servletPathForDefaultMapping="true"/> 元素添加到 server.xml 配置文件。添加此元素将导致 getServletPathgetPathInfo 方法的行为与竞争应用程序服务器中的行为相同。如果使用 Servlet 4.0 实现,那么无需进行任何更改。Servlet 4.0 实现中 getServletPathgetPathInfo 的行为与竞争应用程序服务器中的行为相同。

如果要迁移到 WebSphere Traditional,请将 WebContainer com.ibm.ws.webcontainer.EnableDefaultServletRequestPathElements 属性设置为 true。设置此属性将导致 getServletPathgetPathInfo 方法的行为与竞争应用程序服务器中的行为相同。

有关更多信息,请参阅: