Tomcat Request.getPort() stripping port number - java

Problem:
I am using Java HttpServeltResponse to call sendRedirect but on a particular deployment the URL port number is not included in the HTTP 302 response message.
Details:
I have an application, which uses spring security to authenticate and redirect on success. The redirection URL provided by me is a relative path.
On redirection the spring framework calls sendRedirect on HttpServletResponse passing in the context path along with the relative URL. For example:
HttpServletResponse.sendRedirect("/MyApp/index.html");
This all works fine when we deploy to tomcat instances on various platforms (Windows/Ubuntu etc). However one of our clients is finding that in their deployment, the redirection port is stripped, for example, if the entry point is:
http://server:8082/MyApp/authenticate
The redirection they get is:
http://server/MyApp/index.html - this page does not exist, as it is missing port 8082
Stepping through the code, I can see that the port is determined by asking the Java Request Object, which seems to be supplied by the Web Application Container (i.e Jetty/Tomcat?). In my dev environment it is org.eclipse.jetty.server.Request.
So is this a tomcat configuration issue? We could replicate the same behaviour using Apache2 ProxyPassReverse, but they assure me they are not running Apache2, and their connection is a direct connection to tomcat.
As I can not replicate this issue anywhere, I am really stuck for an answer. Any help would be very much appreciated.

Related

session replication without loadbalancer

I am trying to set up session replication between two tomcat servers(server1,server2) in two different machines.
I am using StaticMembershipInterceptor and StaticMember instead of Multicast. I am not using any loadbalancer.
Below is the site mainly I have followed. I referred this configuration as this is the exact requirement I have.
http://khaidoan.wikidot.com/tomcat-cluster-session-replication-without-multicasting
I am using Tomcat9.
What I am expecting is after the above configuration is:
Server1: Tomcat A
Server2: Tomcat B
Exected Result :
Firststep:
I start the Tomcat A. Load the webpage.(http://server1:8080) Login to application by entering username and password and session is created.
Secondstep:
I start the Tomcat B. Load the webpage.(http://server2:8080). It should automcatically login to the application without entering username and password. No login page should be available.
Actual Result after the above configuration:
In Secondstep: Login page is available. the reason why login page appears is it creates different session.
When I tested the same configuration in the two nodes of same machine, it works fine. But in different machine, it does not work.
What I tried so far:
Checked the log file of two tomcats - I can see the connection is
established.
Made sure has distributable tag
Also added the same in context.xml
Tried adding inside the tag as I saw it in couple of forum.
Checked netstat -ano I could see ports are listening at 4100 in both
the machines
Am I missing any other configuration? Not sure why it does not work.
I'm assuming you are using cookie-based session-tracking, because it is enabled by default and is preferred over URL-based session-tracking in Tomcat if the client is willing to send JSESSIONID cookies.
Cookies are scoped to a particular hostname and path. So, when you login to Tomcat A (http://server1:8080), you get a cookie scoped to server1/path. Wen you visit Tomcat B (http://server2:8080), your cookie for server1 isn't sent and you are challenged to login.
If you want this to work, you will need to choose one of the following options:
Use a reverse-proxy / load-balancer
Use multiple-IP DNS to resolve server (not server or server2) to multiple IP addresses, each pointing to a separate Tomcat instance
Disable cookie-based session tracking
But the way you are testing things cannot work due to the rules browsers follow for HTTP cookie handling.

Deploying Java SE6 stand alone web service on a server

Follwing the steps as outlined here: Standalone web service
I created a test web service that works great on my local machine. Since this is 'stand alone' I copied the same root folder on to a 'server' that I use and published the service on the server (as if it is my local machine). When I access the wsdl using localhost as the domain name, it works fine on the server. However, when I try the url from a different macihne on the network giving the server's domain name instead of localhost, I get a 'can not be displayed' error in IE.
My question is, should this even be possible? Or is there anything specific that needs to be done. Since this is a 'stand alone' solution, we should not require 'another' container like tomcat correct?
To be honest, until your post, I had no idea there was a builtin, lightweight, HTTP Server in the JDK. I've always used glassfish for my web service needs.
I can't say for sure, but if you look closely at the example code you'll see:
Endpoint endpoint = Endpoint.publish("http://localhost:8080/calculator", calculator);
I suspect that this limits you to "localhost" as opposed to the host machine. Try changing it so that it represents the name of the server and try again from another machine (naturally making sure it can get through the firewall as well). Something like:
Endpoint endpoint = Endpoint.publish("http://myserver:8080/calculator", calculator);
Rebuild it and try again. Other than that, you'd need to create a proper war file and deploy to glassfish, tomcat, etc.

How to configure Apache to redirect subdomains to Tomcat applications

I have a few applications hosted on Tomcat running a machine called test-websites throuhg port 8080. So they are accessible like this:
http://test-websites:8080/app1/
http://test-websites:8080/app2/
...
http://test-websites:8080/appN/
What I need to do is make these applications accessible on my local network by:
http://app1.test-websites/
http://app2.test-websites/
...
http://appN.test-websites/
As I add new applications to Tomcat's webapps folder, I want them to be automatically available using the same subdomain pattern.
So I thought using Apache in front of Tomcat to make the URL rewriting would be a good idea, but so far I have not been able to configure the virtual host on Apache to make this redirect. I installed apache2 on port 80 and I see the default "It Works!" apache page when I access http://test-websites/, but I couldn't find how to make the redirects to the apps in the Tomcat following the format above.
I have searched for over 4 hours and didn't get an answer for this use case.. any help us much appreciated!
Thank you!
Eduardo
First you need to add a DNS entry for app1.test-websites, app2.test-websites,.. such that it points to test-websites. Generally CNAME entry works best in this case. If you only need the URLs to resolve on your local machine (for testing purpose), you can just update your /etc/hosts or C:\windows\system32\drivers\etc\hosts file. Otherwise you need to figure out how your company's network is setup and change the DNS entry (if it's a Windows domain network, normally there's a DNS service on the domain controller. On some smaller network you have to configure it on the router).
Next, the quickest way to achieve this is to not use apache2 to front it, bust simply have tomcat listening on port 80. You can setup virtual host on tomcat such that it serves different web-app depending on the URL requested.

URL Rewrite IIS and Glassfish from port 8080 to 80

I'm running a glassfish server that handles all jsp and servlets.
Now there is a way to get it working so that you can run IIS on port 80 and glassfish on port 8080, now the problem is:
When i request http://localhost/myWebApplication it should rewrite the url and use http://localhost:8080/myWebApplication
Basically I make a request to IIS, but the response I get back must be from Glassfish.
When I request the url with port 8080 it works, because its getting it from Glassfish, but i need to go through IIS to Glassfish.
Here is a website that explains it all: http://jstoup.wordpress.com/2012/04/25/how-to-integrate-glassfish-with-iis/
but I still can't get it working.
I have got no idea how my url rewrite rules must be set up.
This is James from the blog you referenced. If you haven't gotten this working you may have to enable your server farm as a proxy. If you select your web farm there should be a proxy config in the center console. All you should have to do is turn that on and you should be good to go. Let me know if you've got any problems

Weird Tomcat and Axis Webservice behaviour

I have a simple web service deployed on tomcat using Apache Axis.
If i access the webservice as http://localhost:8080/webservices/TransactionService i see the usual message
TransactionService
Hi there, this is an AXIS service!
Perhaps there will be a form for invoking the service here...
showing that the web service is available and ready for use.
However if i access it as http://10.0.0.1:8080/webservices/TransactionService (10.0.0.1 is the actual IP of the machine. I'm accessing it on the same machine as above, machine hosting tomcat) i get:
HTTP Status 404 - /webservices/TransactionService
--------------------------------------------------------------------------------
type: Status report
message: /webservices/TransactionService
description: The requested resource (/webservices/TransactionService) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/5.5
There is nothing in the tomcat logs
If i try deploying on Jetty it all works fine.
Is there any explanation for this? Any pointers most welcome.
Tomcat can listen on different hostnames/IPs in a different way. Specifically, every host/IP can have its own work directory:
<Host name="localhost" workDir="/workdir">
...
</Host>
Application deployed to one workdir won't be available to a host with another workdir.
Check your configuration.
UPDATE: if name is specified as name, not IP, check that that name is resolved to 10.* address too.
Also, one of the hosts is default. It responds to all requests now matter what host they are targeted too, if there is no specific Host. For your setup you may want to leave only that one active.
I don't think a change to Tomcat configuration is the answer. I don't have to do such a thing to use my local IP address or localhost.
Could it be as simple as an addition to your hosts file? I've got mine in c:/windows/system32/drivers/etc/hosts, and there's an entry for "127.0.0.1 localhost" in it.

Categories