Servlet 路径信息包含空字符串
Servlet path info includes empty string
我有以下 servlet:
@WebServlet("/publication/topic/*")
public class ViewTopicPublicationsServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
String[] pathInfo = request.getPathInfo().split("/");
System.out.println(Arrays.toString(pathInfo));
...
}
}
例如,如果我有这样的 url:
http://localhost:8080/bulletinboard/publication/topic/SALE
我想省略空字符串。所以 pathInfo
结果是 [SALE]
而不是 [,SALE]
如何实现?
您可以省略第一个字符,因为它始终是斜线:
request.getPathInfo().substring(1).split("/")
我有以下 servlet:
@WebServlet("/publication/topic/*")
public class ViewTopicPublicationsServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
String[] pathInfo = request.getPathInfo().split("/");
System.out.println(Arrays.toString(pathInfo));
...
}
}
例如,如果我有这样的 url:
http://localhost:8080/bulletinboard/publication/topic/SALE
我想省略空字符串。所以 pathInfo
结果是 [SALE]
而不是 [,SALE]
如何实现?
您可以省略第一个字符,因为它始终是斜线:
request.getPathInfo().substring(1).split("/")