Virtual hosting on Glassfish
June 27, 2009 6 Comments
I’ve been looking how to change the URL of a website I’m developping in JEE. You probably have noticed that the URL of the project You’re developping is something like : http://localhost:8080/theContextPathOfYourProject.
I’m currently developping my application with NetBeans and Glassfish V2 and I would like to change this URL with something more simple, like http://www.mysite.com/
So I checked out and a nice feature of Glassfish could be used : Virtual Hosting.
This article will show You how to do that, in the easiest way possible. So let’s go.
First of all, You’ll need to have a JEE application. With this application, You can change its context-path to something simple. For example, my application is named jacc so I changed its context-path to jacc. Do this is pretty simple.
In NetBeans, open the Projects window, right-click on Your application and choose Properties. In the new window, go to Run and change the context-path, as shown below :
After that, we’re going to work in the Administration console of Glassfish. So if Glassfish isn’t started yet, do it and go to the admin console.
We will create a virtual server. So let’s go to Configuration, HTTP Service and Virtual Servers. You should be there :
Click on the New… button. Give an id (for example jacc), the hosts (which corresponds to the address You’ll enter in Your browser to access Your website : I want jacc.com). Hit the OK button. We’ll create a listener and then, go back here.
Step 2 :
Here we will create a listener. This listener will listen requests on a given port. The default port Glassfish is listen to is 8080. If You don’t want to run Your app on another port, You can go to step 3. Otherwise, let’s go.
We’re going to create a listener for the port 8282. On the left of the admin console, click on Configuration, then HTTP Service and HTTP Listeners. You should be there :
Right, click on the New… button. Give Your listener a name such as http-listener-3, the network address (in our case, for localhost, we enter 0.0.0.0), the listening port (8282 as we said) and the default virtual server, created before. So in the list You should see jacc. If not, restart Glassfish and restart this step. Hit Next and the Finish.
Then go back to the virtul servers list and click on the one You’ve created before. And in the text field for the HTTP Listners, enter the name of the listener You’ve created before. For me http-listener-3.
Then, in the list of Default Web Module, You will choose Your web application. Mine is called jAcc-war. So I select it. With that, You won’t need to type the context path of Your application in the URL🙂
Ok, should be nice. Now, we have to indicates Glassfish our web application will run on our brand new virtual server.
So, go to Applications, Web Applications and select Your application. Mine is jAcc-war. Then select Your virtual server in the list like this :
So now, restart Your Glassfish. Then we’re going to work somewhere else…
When You type the hosts address You defined in the creation of Your virtual server (jacc.com for me) in the address bar of Your browser, it shouldn’t go to the right place… So You have to tell Your computer that this host is Your computer. So find a file named hosts in Your system. In Unix based OS, it should be in /etc/. On Windows system : C:\WINDOWS\system32\drivers\etc. Edit this file and add a line like this :
Well, now it should work🙂 So in Your brower, You should type the URL You wanted and the port on which Your virtual server listens. So for me jacc.com:8282
So it is more convenient, isn’t it?🙂
So now You know how to deploy virtual servers using Glassfish.