如何在servlet中读取变量URL

时间:2014-07-19 12:23:54

标签: java url servlets

我正在使用servlet,我正在使用此URL

http://s4.com/api/system?section0=report&reportType0=overTime&dataType0

在浏览器中请求我在servlet中创建的一些json数据

我尝试阅读整个网址,以使其成为s4.com方法中的第一部分doGet,因为它可能是:

http://s.com/api/system?section0=report&reportType0=overTime&dataType0
http://s1.com/api/system?section0=report&reportType0=overTime&dataType0
http://s2.com/api/system?section0=report&reportType0=overTime&dataType0
http://s3.com/api/system?section0=report&reportType0=overTime&dataType0
http://s4.com/api/system?section0=report&reportType0=overTime&dataType0
.
.
.
http://s100.com/api/system?section0=report&reportType0=overTime&dataType0

所以我需要使用s(number).com

来区分它们

在我尝试的do get方法中

request.getRequestURL().toString(); ===== output===> http://localhost:8090/MySim/api/system

request.getServerName().toString();===== output===>  localhost

request.getRequestURL().toString(); ===== output===> /MySim/api/system

我也试过这个

        String uri = request.getScheme() + "://" +   // "http" + "://
    request.getServerName() +       // "myhost"
    ":" +                           // ":"
    request.getServerPort() +       // "8080"
    request.getRequestURI() +       // "/people"
    "?" +                           // "?"
    request.getQueryString(); 

打印完整路径URL   =====输出===>

http://localhost:8090/MySim/api/systemsection0=system&type0=shortStatus&rand0=1405835810247&sessionId=FFFFFFFFF

但其中没有得到正确答案

我是web.xml的一部分:

<servlet-mapping>
    <servlet-name>Api</servlet-name>
    <url-pattern>/api/system</url-pattern>
</servlet-mapping>

主持文件:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost

127.0.0.1       localhost
127.0.0.1       sim.localhost
127.0.0.1       sim.nma.localhost
127.0.0.1       nma.localhost
127.0.0.1       s.com
127.0.0.1       s1.com
127.0.0.1       s2.com
127.0.0.1       s3.com
127.0.0.1       s4.com
127.0.0.1       s5.com

我正在使用带有apatchi的xhamp服务器,因此在Config文件中我习惯了以下内容:

</VirtualHost>
        <VirtualHost *:80>
           ServerName s4.com


          Header add Access-Control-Allow-Origin "*"
            ProxyRequests Off
            <Proxy *>       
                Allow from all
            </Proxy>

            ProxyPass /session/             http://localhost:8090/MySim/session/
            ProxyPass /api/                 http://localhost:8090/MySim/api/


            ProxyPassReverse /session/      http://localhost:8090/MySim/session/    
            ProxyPassReverse /api/          http://localhost:8090/MySim/api/    


        </VirtualHost>
        <VirtualHost *:80>
           ServerName s5.com


          Header add Access-Control-Allow-Origin "*"
            ProxyRequests Off
            <Proxy *>       
                Allow from all
            </Proxy>

            ProxyPass /session/             http://localhost:8090/MySim/session/
            ProxyPass /api/                 http://localhost:8090/MySim/api/


            ProxyPassReverse /session/      http://localhost:8090/MySim/session/    
            ProxyPassReverse /api/          http://localhost:8090/MySim/api/    


        </VirtualHost>

这就是request.getserverName()始终打印本地主机

的原因

现在我编辑了以下配置文件:

</VirtualHost>
        <VirtualHost *:80>
           ServerName s4.com


          Header add Access-Control-Allow-Origin "*"
            ProxyRequests Off
            <Proxy *>       
                Allow from all
            </Proxy>

            ProxyPass /session/             http://s4.com:8090/MySim/session/
            ProxyPass /api/                 http://s4.com:8090/MySim/api/


            ProxyPassReverse /session/      http://s4.com:8090/MySim/session/   
            ProxyPassReverse /api/          http://s4.com:8090/MySim/api/   


        </VirtualHost>
        <VirtualHost *:80>
           ServerName s5.com


          Header add Access-Control-Allow-Origin "*"
            ProxyRequests Off
            <Proxy *>       
                Allow from all
            </Proxy>

            ProxyPass /session/             http://s5.com:8090/MySim/session/
            ProxyPass /api/                 http://s5.com:8090/MySim/api/


            ProxyPassReverse /session/      http://s5.com:8090/MySim/session/   
            ProxyPassReverse /api/          http://s5.com:8090/MySim/api/   


        </VirtualHost>

1 个答案:

答案 0 :(得分:0)

您的客户端浏览器连接到

http://s4.com/api/system

但您的网络应用程序会响应

http://localhost:8090/MySim/api/system

这是一个常见的用例:它意味着你有一个HTTP反向代理(公共是apache,nginx或IIS)来进行URL重写。如果可能,您应该询问代理的管理员如何以及在何处找到原始请求的主机。如果这不是一个选项,您将必须检查所有标头以尝试发现它。例如,Apache mod-proxy通常使用标头X-Forwarded-Host