Get server Port in web application at server start - java

I need to retrieve server information like Server IP and Port at server startup.
I am using Spring and hibernet in my project and Glassfish (and tomcat) is the application server.
I know that I can get the IP and Port information from request, but there is no request at server startup.
I found on net after some search that IP address can be retrieved as follows:
InetAddress.getLocalHost().getHostName();
but I did not find any method to retrive the current Port of the server. I am hoping to find API from app server which will provide this information. I am using Spring in my application and thus any indications from Spring API will also be helpful.

Well you should configure port in your web.xml as param and read it out in your code using ServletContext or you can use this poor hack

A server may be listening to multiple ports on multiple names, so you cannot be certain that the one chosen automatically is the one you really want.
Question is what you need it for.
If it is for giving URL backs in requests then use the information in requests.
If it is for logging or announcement with ZeroConf, then consider writing application server specific code asking it about its configuration.
If all else fails, explicitly pass in the information through e.g. system properties or JNDI.

Related

How to retrieve port number from a tomcat server sitting behind a load-balancer

Situation:
My client has a Java web application deployed on a JBoss server, it is accessed both via HTTPS and HTTP
The JBoss server is sitting behind a load-balancer that handles the SSL, in other words this load-balancer terminates SSL and sends requests to JBoss as plain HTTP
Problem I need to solve:
The web application deployed on JBoss needs to know what port the load-balancer is using for HTTPS, so that it can direct users to certain HTTPS urls correctly. This cannot be hard-coded because the application will be deployed on multiple clients, each of them have different configurations for the load-balancer.
My approach (which didn't work):
I am defining the port from a jspx page, via ${pageContext.request.serverPort}, but this always returns the port for HTTP because JBoss always gets the request via HTTP.
Thanks in advance. I've looked at this question but was not helpful.
There isn't really a way to determine the port if tomcat is sitting behind a load balancer. As suggested in the comments, exposing the port configuration to your clients would be the best for now.

Java Webservice hitting proxy instead of destination on Apache Tomcat

I have a java webservice running on Apache Tomcat 7.0.39. It receives a message from another server on the same network then tries to send an outgoing message back. This outgoing message is being sent to NAProxy instead of the server it's supposed to.
I've turned off proxy settings in IE and the Java control panel. I've tried adding a ProxyServer variable to the cxf-beans file:
<http-conf:conduit name=".*http-conduit">
<http-conf:client ConnectionTimeout="20000" ReceiveTimeout="30000" ProxyServer=""/>
</http-conf:conduit>
but neither of these things work.
IE can reach the destination service from the outgoing server. I can hit the hosted destination service from the server using SOAPUI, so it's just a problem with the apache service, not the box.
It seems it is defaulting to use the proxy instead of a direct connection. Is there a setting somewhere to turn off proxy use?
Proxy can be set also using system properties http.proxyHost and http.proxyPort. I don't know how your application is being started, but maybe somewhere there are JVM parameters -Dhttp.proxyHost=... -Dhttp.proxyPort=...
Since it is Tomcat, maybe you should check startup file.

Moving webapp from localhost to server - page cannot be found

I have developed a Play! 1.2.7 web application which calls an existing online payment service, eclipsified to work in Java.
I send data in POST to a servlet and get a response in GET to my application. This is possible because one of the data sent to the service is the URL of my controller handling the response.
I had this working perfectly in local environment but when I move the webapp to a Tomcat container on our local server with public ip I'm stuck because service response cannot find my webapp as it used to do with localhost.
Locally the URL was something like
http://localhost:port/myAction
When I put it on tomcat I changed the URL to
http://my.public.ip.address:tomcat_port/app_name/myAction
I get the "page is not available" message from browser.
My guess is that I miss some basics from network communication and I'd like to know how to have this working when the application is deployed on our server.
Ps. sorry for bad tagging, I'm not sure what's this question's target
EDIT: no logs from catalina.out. If I run the application nothing gets logged
I came across the solution by knowing that loopback is not enable by default on every router.
So I will never be able to have a response client side when I send the request from the lan where the server is located, unless loopback is enabled on the router.
To test my application I had to use the server local address, visible from inside.

How do I set up Glassfish to go via a proxy server?

I have been having issues with running a Glassfish v2.1.1 instance on my local machine from within the office, where we have a proxy server for outgoing connections. My initial workaround has been to work from home.
I am calling a SOAP service on a HTTPS server outside of the company. As Glassfish is not going via the company's proxy server, I get the following error when trying to initialise my SOAP clients:
javax.xml.ws.WebServiceException: Failed to access the WSDL at: https://www.hostname.com...
and
Caused by: java.net.UnknownHostException: www.hostname.com
I have proxy environment variables set on my command line, as well as my system proxy settings all working correctly so that I can get to the WSDL with the browser. How should I configure Glassfish?
I had a lot of trouble finding an answer to this, as the topic isn't covered in a lot of detail on the web. One link told me how to configure the HTTP proxy, but mentioned nothing about HTTPS, so it took me a while to figure it out.
Open up the admin console on your Glassfish server and go to:
Application Server -> JVM Settings -> JVM Options. Click "Add JVM Option" 4 times and enter the following 4 options
-Dhttp.proxyHost=proxyhostname
-Dhttp.proxyPort=8080
-Dhttps.proxyHost=proxyhostname
-Dhttps.proxyPort=8080
Where proxyhostname and the port number are correct for your setup. Then you need to restart the server.
Note that I couldn't find any options for setting up the proxy from a PAC file, nor for proxies which require auth. In this case, you may need to install a local auth proxy handler like Authoxy for Mac OS X, which turns your localhost into a non-auth proxy and masks the authentication request from the central auth proxy.
Also, this link was good for various proxy options to the JVM:
http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
You have to explicitly set the proxy server. Several options are available depending on the Glassfish version. In general you can set the proxy by either using JVM arguments or the Glassfish Admin console. An intro for GF3 is available here (sorry for only providing a link, but I do not want to repeat all the details here).
Just to make the answer complete, if the proxy requires user name and password, set the following in Glassfish:
-Dhttp.proxyUser=someUserName
-Dhttp.proxyPassword=somePassword
The host cannot be resolved, are you sure you use a hostname resolvable by an internet DNS server or are you using something that can only be resolved from within your network or even worse, a hosts entry local to your machine?
Aside from that, the proxy server might be denying you access to some ports, but this is probably not your problem right now. If the proxy allows access to port 80, try running your Glassfish on port 80 as well if you get any connection timeout errors.

Java EE getting servlet container port

I want to programmatically get the servlet containers port that my Java EE application is deployed on. I assumed there would be something in the JMX beans but I can't seem to find anything.
And before anyone says grab the port from the HttpRequest or HttpResponse it should be noted that this process is running behind the servlet and has no interaction with the Requests or Responses.
One possible "hack" would be to parse the server.xml at runtime and identify the port that is configured.
But looks like there is a way to do it using JMX / MBeans as well.
I happened to have a strong need to extract the tomcat port from request. So that I can compare and tell if the servlet request is from tomcat port http://localhost:8080 or from apache port http://localhost/
By doing this, I can make sure the request only be processed by strictly using tomcat port and rejecting by apache port. This may help setup security configuration that the request would not allow outside world access via apache. tomcat port only available within local network.
The method is to create a tomcat valve (assuming you know what it is) implements org.apache.catalina.valves.ValveBase.
This interface provides org.apache.catalina.connector.Request as an argument.
Per request:
using request.getConnector().getPort() which returns 8080. Then say.
if ( request.getServerPort() != request.getConnector().getPort() ) {
response.getWriter().print("<span>access denied</span>");
} else {
getNext().invoke(request, response);
}

Categories

Resources