How to know if my application is running over a JBoss server? - java

I am developing a GWT application.
And I would like to know if I am running over a Jboss instance or over a Jetty instance.
This is because if I am running over Jetty it means that I am running the dev mode and I need to redirect to MyModule.html?codeserver... but if I am running over Jboss I need to redirect to MyModule.html
But I can't figure how can I know if I am running over Jetty or over Jboss.

You should not try to find out the webserver on which your application is running, in your application code. Instead you can have a discriminating parameter set in the context of your application with different values for different servers.
E.g.
For JBoss, the server.xml can contain a parameter in the context definition as follows:
<Context ...>
...
<Parameter name="applicationStage" value="prod" />
...
</Context>
and for Jetty, the same context parameter would go into its jetty.xml but the value as "devo".

If you just want to know wether you are running production or development mode, then try
GWT.isProdMode();
Only works client-side of course.

There's not really any simple way to determine what application server you're running on (to my knowledge). You could check some system properties to determine this.
JBoss:
jboss.server.name
Jetty:
jetty.home
Although I do not encourage having logic determined by your environment as Vikdor mentions in his answer. His suggestion would be a better approach.

In a JSP,
Server is <%= application.getServerInfo()%>
or use
Server is ${pageContext.servletContext.serverInfo}
in a Servlet,
String server = getServletContext().getServerInfo();

Related

Going from a local testing environment to live site

So, I've been developing my first actual site and so far I've been developing it on a local webserver, Apache Tomcat 7.0 along with a MySQL server, and I'm wondering what changes or problems I should be expecting when I go from that testing environment to a live VPN web server which I've been told I'll have to switch over to.
If it makes any difference I'm using struts2 for my action mapping which I've noticed append all my URL's with .action ex: localhost:8080/pm/random.action
However the expected url is, busniessname.com/pm/random
So the tl;dr, what should I be expecting when I switch from a local testing environment to the live website and what should I do to fix the URL's?
You should do nothing, the urls without .action extension should work by default. If you would like to use an empty extension only then you should set the property to the configuration.
<constant name="struts.action.extension" value=",,"/>

Map different url to same web application in Tomcat

I am not clear on the following:
If we have a web application named: SomeWebApp under Tomcat's webapp directory, the url to access it is:
http://localhost:8080/SomeWebApp
My question is, is it possible to configure Tomcat so that other URLs would point to that web application?
E.g.
http://localhost:8080/ADifferentApp will also point to the SomeWebApp?
From the web.xml I think is not possible since it is about the url patterns when you are inside the SomeWebApp scope.
So what is the proper way to do it? If it is possible that is.
The approach I found to work best is to install Apache2 on the server and proxy all requests. Tomcat is surprisingly difficult to configure in other ways than intended. In my experience, Tomcat doesn't provide this functionality declaratively.
I'd rather recommend Nginx than Apache as proxy. I'm recently working on a project that incorporates tomcat and nginx works as proxy.
Once you've got nginx you can acctualy map as many url's to access the same web application as you want.
Yes,its possible to map different context path to single application edit conf/server.xml file
> **> <Context docBase="D:\Servers\apache-tomcat-7\webapps\SomeWebApp"
> > path="/SomeWebApp" />
> > <Context docBase="D:\Servers\apache-tomcat-7\webapps\SomeWebApp" path="/ADifferentApp "/>**
Access application with 2 URL's

Setting Runtime Property in Web Application

I am planning to use JGroups in a web application.
JGroups by default uses IPv6 for multicasting of messages. JGroups can only be configured to use IPv4 by setting a property like the following (see docs)
-Djava.net.preferIPv4Stack=true
This does not work if set in code. What are my options when running a war file in an application server for setting this property, specifically tomcat and glassfish?
It seems to me the following route might work. First, add a ServletContextListener to your web app and register it in web.xml before JGroups is loaded.
Now, inside the contextInitialized method, use System.setProperty("java.net.preferIPv4Stack", "true").
Disclaimer: not tested.
Unless you have a specific reason to put this in code, it can easily be configured for Tomcat by setting the environment variable CATALINA_OPTS (to -Djava.net.preferIPv4Stack=true). I think the proper place to do this is to create ${CATALINA_HOME}/bin/setenv.sh and place the setting in there.

Short url or alias for deployed application in tomcat 6

I have a web application project which is deployed in tomcat 6.
I can access my application using the url:
http://localhost:8082/MyApplication
I also wan't to be able to access this application by another url like:
http://localhost:8082/myapp
Is this possible ? if yes what alternatives do i have ?
Off course, I don't want to change the original name of the application('MyApplication').
Thanks,
Abhishek.
If you add the Context within server.xml it will work as you want. Give the path attribute you wish.
<Context docBase="MyApplication" path="/myapp" />
Though it works, this approach is not recommended by the Tomcat docs, since any changes to server.xml means restarting the server disturbing all the web apps.
But, on the flip side, the practice of keeping this in Catalina_Home/conf/Catalina/localhost/context.xml (which is recommended by the docs) has some unreliabilities as others have reported - when you redeploy the war you can lose the context.xml too
See Why-does-tomcat-replace-context-xml-on-redeploy and
Why does tomcat like deleting my context.xml file?

Resolving Session Fixation in JBoss

I need to prevent Session Fixation, a particular type of session hijacking, in a Java web application running in JBoss. However, it appears that the standard idiom doesn't work in JBoss. Can this be worked around?
This defect (found here) points the way to the solution. The Tomcat instance that runs in JBoss is configured with emptySessionPath="true", rather than "false", which is the default. This can be modified in .../deploy/jboss-web.deployer/server.xml; both the HTTP and AJP connectors have this option.
The feature itself is used to eliminate the context path (eg. "foo" in http://example.com/foo) from being included in the JSESSIONID cookie. Setting it to false will break applications that rely on cross-application authentication, which includes stuff built using some portal frameworks. It didn't negatively affect the application in question, however.
This problem and the specific case in which it occurs is a problem in Tomcat as well as JBoss. Tomcat shares the emptySessionPath="true" effect (and actually JBoss inherits it from Tomcat).
This really seems like a bug in Tomcat and JBoss when you are trying to prevent session fixation attacks but the servlet spec (at least version 2.3) does not actually require the JSESSIONID to be defined or redefined according to any specific logic. Perhaps this has been cleaned up in later versions.
One workaround is to store the client address in the session. A response wrapper should validate the client address set in the session is same as the one accessing the session.
I came to know below code setting snippet from one of the forum. And I added below lines. But when I print the session ID after and before log in into the application it is same. How would I test session Fixation.
D:\jboss-5.1.0.GA\bin\run.cof file and add the below line.
set "JAVA_OPTS=%JAVA_OPTS% -Dorg.apache.catalina.connector.Request.SESSION_ID_CHECK=false"
in each context.xml of the jboss applications.
D:\jboss-5.1.0.GA\server\default\deploy\jbossweb.sar\context.xml

Categories