我想从一个servlet获取我的Web应用程序的根URL。Servlet的根URl
如果我在“www.mydomain.com”部署我的应用程序,我想获得像“http://www.mydomain.com”这样的根网址。
同样的事情,如果我部署它在8080端口本地Tomcat服务器,它应该给http://localhost:8080/myapp
谁能告诉我如何从我的servlet的web应用程序的根URL?
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String rootURL="";
//Code to get the URL where this servlet is deployed
}
}
字符串使用rootUrl = request.getRequestURL()的toString()代替(request.getRequestURI(), “”); – Sllouyssgort 2015-08-20 20:42:57
@Sllouyssgort不起作用,因为'getRequestURI()'包含'myapp' – Black 2018-02-08 04:05:35