servlet的根URl [英] Root URl of the servlet

查看:91
本文介绍了servlet的根URl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从其中一个servlet获取我的Web应用程序的根URL。

I want to get the root url of my web application from one of the servlet.

如果我在www.mydomain.com中部署我的应用程序,我想要获取 http://www.mydomain.com 之类的根网址。

If I deploy my application in "www.mydomain.com" I want to get the root url like "http://www.mydomain.com".

如果我在8080端口的本地tomcat服务器上部署它应该给出 http:// localhost:8080 / myapp

Same thing if I deploy it in local tomcat server with 8080 port it should give http://localhost:8080/myapp

有人能告诉我如何从servlet获取我的Web应用程序的根URL吗?

Can anyone tell me how to get the root URL of my web application from servlet?

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

    }
}


推荐答案

您确实意识到URL客户端看到(和/或键入其浏览器),并且您的servlet部署在容器上的URL可以是非常不同?

You do realize that the URL client sees (and/or types into his browser) and the URL served by the container your servlet is deployed on can be very different?

但是,为了获得后者,你可以在 HttpServletRequest

In order to get the latter, though, you have a few methods available on HttpServletRequest:


  • 您可以调用 getScheme() getServerName() getServerPort() getContextPath()并使用适当的分隔符组合它们

  • 或者你可以调用 getRequestURL ()并从中删除 getServletPath() getPathInfo()

  • You can either call getScheme(), getServerName(), getServerPort() and getContextPath() and combine them using appropriate separators
  • OR you can call getRequestURL() and remove getServletPath() and getPathInfo() from it.

这篇关于servlet的根URl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆