how to read variable URLs in servlet - java

i am using servlet, and i am using this URL
http://s4.com/api/system?section0=report&reportType0=overTime&dataType0
in the browser to request some json data that i create in the servlet
i tried to read the whole URL to get it is first part s4.com in the doGet method since it may came as :
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
so i need to distinguish between them using the s(number).com
in the do get method i tried
request.getRequestURL().toString(); ===== output===> http://localhost:8090/MySim/api/system
and
request.getServerName().toString();===== output===> localhost
and
request.getRequestURL().toString(); ===== output===> /MySim/api/system
and i also tried this
String uri = request.getScheme() + "://" + // "http" + "://
request.getServerName() + // "myhost"
":" + // ":"
request.getServerPort() + // "8080"
request.getRequestURI() + // "/people"
"?" + // "?"
request.getQueryString();
which prints the full path URL
===== output===>
http://localhost:8090/MySim/api/systemsection0=system&type0=shortStatus&rand0=1405835810247&sessionId=FFFFFFFFF
but non of these got the right answer
my a part of web.xml:
<servlet-mapping>
<servlet-name>Api</servlet-name>
<url-pattern>/api/system</url-pattern>
</servlet-mapping>
hosts file :
# 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
i am using xhamp server with apatchi so in the Config file i used to the following :
</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>
this is why request.getserverName() always prints local host
and now i edited the config file ass following :
</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>

Your client browser connects to
http://s4.com/api/system
but your web applications responds to
http://localhost:8090/MySim/api/system
That is a common use case : it means that you have a HTTP reverse proxy (commons are apache, nginx or IIS) that does the URL rewriting. If it is possible, you should ask the administrator of the proxy how and where you can find the original requested host. If this is not an option, you will have to examine all the headers to try to discover it. For example, Apache mod-proxy normally uses the header X-Forwarded-Host.

Related

Using mod_proxy_ajp for a java portal on Tamcat+Apache

I have a java portal running on Tomcat but I want its static contents(except html) to be served by Apache httpd.
So I've installed an Apache httpd and now I am configuring httpd.conf
I know that I need something like the below text:
<VirtualHost *:80>
DocumentRoot /opt/tomcat/webapps/ROOT
ServerName mywebapp.com
ServerAlias mywebapp.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPassMatch "^/(.*\.gif)$" "ajp://localhost:8009/$1"
ProxyPassReverse / ajp://localhost:8009/
But this is a sample and I do not know how to use RegEx in front of ProxyPassMatch to fulfill my purpose.
My purpose is to serve jpg, jpeg, gif, js, css by Apache httpd
And others serving by Tomcat
I found a solution:
<VirtualHost *:80>
DocumentRoot /opt/tomcat/webapps/ROOT
ServerName mywebapp.com
ServerAlias mywebapp.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
#ProxyPass / ajp://localhost:8010/
ProxyPassMatch ^/(.*(?<!\.jpg)(?<!\.png)(?<!\.jpeg)(?<!\.css)(?<!\.ico)(?<!\.bmp)(?<!\.js)(?<!\.gif))$ ajp://localhost:8009/$1
ProxyPassReverse / ajp://localhost:8009/
</VirtualHost>

Removing app name from url in proxypassreverse setup

I have been able to map my application to the domain in a manner I found after doing some research (copy-paste) on internet.
<VirtualHost *:80>
ServerName example.com
ProxyPass /MyApp http://localhost:8080/MyApp
ProxyPassReverse /MyApp http://localhost:8080/MyApp
ProxyPassReverseCookieDomain localhost example.com
ProxyPreserveHost On
RewriteEngine on
RewriteRule ^/$ http://example.com/MyApp
</VirtualHost>
The rewrite works fine and whenever I open example.com it redirects to example.com/MyApp . My query is, how can I make it not redirect and deliver the MyApp at example.com/ without any redirect. Just plain simple mapping of the MyApp to the root of the domain. The rewrite works fine, but certainly don't want app name in url.
Rename your war file to ROOT.war to make it the root webapp in Tomcat. Then adjust the paths accordingly in the Apache config:
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/

Glassfish and Tomcat in same server machine and hosting more than one site on both?

I have a tomcat 6.0 web server on my server machine.now i created a glassfish server on the same machine. for avoid port conflict i just change glassfish port to 8081. now how can i access my web applications on both glassfish and tomcat from different DNS name.
Depends on your DNS/server setup.
You could use Apache and reverse proxies:
<VirtualHost *>
ProxyPreserveHost On
# Servers to proxy the connection, or;
# List of application servers:
# Usage:
# ProxyPass / http://[IP Addr.]:[port]/
# ProxyPassReverse / http://[IP Addr.]:[port]/
# Example:
ProxyPass / http://0.0.0.0:8081/
ProxyPassReverse / http://0.0.0.0:8081/
ServerName glassfish.server
</VirtualHost>
<VirtualHost *>
ProxyPreserveHost On
# Servers to proxy the connection, or;
# List of application servers:
# Usage:
# ProxyPass / http://[IP Addr.]:[port]/
# ProxyPassReverse / http://[IP Addr.]:[port]/
# Example:
ProxyPass / http://0.0.0.0:8080/
ProxyPassReverse / http://0.0.0.0:8080/
ServerName tomcat.server
</VirtualHost>
Or you could use your DNS. I'm afraid I can't help you with that.

Apache ProxyPass Tomcat

I have some trouble with Apache and Tomcat server inside same machine.
I want redirect a virtual host, kb.domain to an tomcat app kb.
I have read some post on internet but I don't found a solution to my problem.
My configuration have one Apache server (http://domain) and in same machine an tomcat server (http://domain:8080); in my Apache I have mapped a VirtualHost that respond to kb.domain like this:
<VirtualHost *:80>
ServerName kb.domain
<Location />
ProxyPass http://192.168.200.3:8080/kb
ProxyPassReverse http://192.168.200.3:8080/kb
</Location>
</VirtualHost>
When I call the kb.domain url from browser he add an extra / at the end and go into redirect loop.
Can anyone help me?
Thanks
Your proxpass directives should be:
ProxyPass / http://192.168.200.3:8080/kb/
ProxyPassReverse / http://192.168.200.3:8080/kb/
try
<VirtualHost *:80>
ServerName kb.domain
ProxyPass /kb http://localhost:8080/kb
ProxyPassReverse /kb http://localhost:8080/kb
</VirtualHost>
If you want to pass regardless of path ( aka not /kb )
<VirtualHost *:80>
ServerName kb.domain
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
</VirtualHost>

PhpMyAdmin not access able after setting tomcat on port 80

I just set tomcat on port 80
by adding this on /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
ServerAdmin tomcat#something.com
ServerName something.com
ServerAlias www.something.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://something.com:8080/
ProxyPassReverse / http://something.com:8080/
ErrorLog logs/something.com-error_log
CustomLog logs/something.com-access_log common
</VirtualHost>
But now i am trying to access something.com/phpMyadmin
It is redirecting to something.com:8080/something.com
which is a wrong path
Please help
Try adding this line right before your existing ProxyPass:
ProxyPass /phpMyadmin !
It tells Apache not to proxy this path to Tomcat. Everything else will be proxied.

Categories