Virtual hosting on Glassfish

Hi everyone,

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 :
Image 2

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.

Step 1:
We will create a virtual server. So let’s go to Configuration, HTTP Service and Virtual Servers. You should be there :
Image 4

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 :

Image 3

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.

Step 3:
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.

Step 4:
So, go to Applications, Web Applications and select Your application. Mine is jAcc-war. Then select Your virtual server in the list like this :
Image 5

So now, restart Your Glassfish. Then we’re going to work somewhere else…

Step 5:
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 :
127.0.0.1    jacc.com

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.
Enjoy :)

Eclipse Galileo

Hello everyone,

The latest version of Eclipse is out :) Codename : Galileo. So be sure to grab it on : http://www.eclipse.org/
If You’re on Mac OS X : Leopard, You will prefer the Cocoa version of the IDE :)

Then, let me give You a little tip : if You’re on Mac OS X, and even if You’ve setted up the JDK 1.6 by default, Eclipse won’t recognize it, and will still use the JDK 1.5. So to change that, go to the Preferences of Eclipse and then to Java, Installed JREs. You should be there :

Image 1

So click on the Add button. In the new window, select MacOS X VM as JRE type. Click next and now enter something like this in the JRE Home text field : /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
Right, You’ve done the most tough part of it :)
Enter an explicit name like JRE 1.6 for example and hit the Finish button. Now You just have to select the new JRE in the list.
Now under the Installed JREs node, select Your execution environment as shown bellow :

Image 2

Well, it will soon be ok :) Just setup the compiler now. Go under Java, Compiler and select 1.6 in the list, as shown bellow :

Image 3

That’s all :)
Enjoy.

Main creation shortcut in NetBeans

Hello everyone,

Maybe You used Eclipse some days (and still use it ;) ) and appreciate the way You can create the main method. In Your class, You enter “main” and then press Ctrl+Space and it generates the body of Your main method.
Using NetBeans, it isn’t available by default. But some times ago, I found a way to do it in the similar way that Eclipse does :) So let’s see how.

You have to create a code template, but don’t be affraid, it’s really simple. First go to the preferences of NetBeans, then in the “Editor” part and click on the tab “Code templates”. You should be there :

Image 1

Then click on “New”. In the window, type the text You have to write. It’s Your shortcut :) For the main method generation, type “main” :) It’s not original but instinctive :)
In the list, Your new abbreviation is selected. In the text area called “Expended Text”, enter this :

public static void main(String[] args) {
${cursor}
}

You’ve successfully created a main template :)
Now in Your class, just type “main” and press the Tab button, and Your main body will be created :)
It’s nice, it’s simple, it’s Java :)
Enjoy

Create Your own Filter in JSF

You may have to restrict access to some pages of your website in JSF and redirect the user to a specific page. For example, if the user is not logged in, he can not access the admin page, and You would like to redirect him to the login page. To do so, You can create Your own filters, and guess what : it’s not tough :)
Let’s see some example.

First of all, You have to implements javax.servlet.Filter. So far away, just create the class, and You’ll have to define three methods : init(),  doFilter() and destroy(). So here we go :

Filter example

Once we have our filter, we have to set it up :) So we do it now.
So go to your web.xml file, and first set the filter markup :
Image 4

Give a name to your filter, and indicates  the class that represents Your filter, typically the class You’ve created and which implements Filter.
And then, You have to indicates on which request this filter is applied to :
Image 5
Note that we use the same name (case sensitive) that we used in the “declaration” of our filter.

It’s done :) Nothing more, nothing less. Feel free to customize this example by defining your rules of restriction :)
Enjoy :)

Java update on Mac

Hello everyone,

June, 15th 2009 : Apple released an update for Java on Mac for Mac OS X Tiger and Leopard : http://bit.ly/3NRDS.
This update brings more reliability, security and compatibility with Java SE 6 and previous version. But the thing I appreciate is the availability of the skin Nimbus (released with the Java 1.6 update 10 on others platforms) on Mac OS :)
I like this brand new skin Sun made some month ago. So now, You can set this skin up for your Java application.
If You don’t know how to do that, look at this, it’s pretty simple:)

For example, set the skin, directly in the main method, before creating any Swing component (JFrame, JWindow…)

try {

// Try to set the Nimbus look’n'feel
// You have to do it in a try block, in case the skin isn’t found
UIManager.setLookAndFeel(“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”);

} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
} finally {

// Start your GUI. If the skin isn’t found, the default one is used

}

Scale an image to fit your screen

We’re going to see how to scale an ImageIcon to fit to your screen. The important point here is to respect a ratio, the one of the ImageIcon.
So imagine somenone send You an image through an OutputStream (one of my previous topic :) ) and You want to display it on your screen, but adapt it to your screen respecting a ratio. The harder thing here is to do math :) So let’s go.

First, we’ll need two variables for the both scaled width and height :

int scaledWidth, scaledHeight;

Our ImageIcon is called myImage. We’re going to first retrieve its original size :

int originalWidth = myImage.getIconWidth();
int originalHeight = myImage.getIconHeight();

// Then calculate the ratio
float imageRatio = (int) (originalWidth / originalHeight);

Then, we’re doing the same but with your screen size :

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
float screenRatio = (float) (screenSize.width / screenSize.height);

Now we have to compare the both ratios and find the right size for our image :)

if(imageRatio <= screenRatio) {
// The scaled size is based on the height
scaledHeight = screenSize.height;
scaledWidth = (int) (scaledHeight * imageRatio);
else {
// The scaled size is based on the width
scaledWidth = screenSize.width;
scaledHeight = (int) (scaledWidth / imageRatio);
}

Nice !! We just have to get a scaled image now. That’s easy :

myImage = new ImageIcon(myImage.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_FAST));

Now it’s done.
Enjoy :)

Send a screenshot through an OutputStream

Maybe have You ever tried to take a screenshot of Your screen in Java, and send it through an OutputStream (for example a Socket).
So we’re going to see how to do that in a simple way.

First of all, You have to take a screenshot of Your screen. That is as easy to do as writing in the console :) We will use the class Robot. So, start by creating an instance of this class :

Robot myRobot = new Robot();

The Robot class contains a method named createScreenCapture() which take a Rectangle in parameter. This Rectangle corresponds to the area You want to take a screenshot. So typically, we’ll take the complete screen resolution. So let’s do that :)

int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height; // get the height of the screen
int screnWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
// get the width of the screen Rectangle
myRect = new Rectangle(screenWidth, screenHeight);
// Take the screenshot
BufferedImage screenShot = myRobot.createScreenCapture(myRect);

Now we’ve got the screenshot, we have to send it through an OutputStream. There is several ways to do it. First, the simpliest of them, should be (You think) to use an ObjectOutputStream and pass the BufferedImage in parameter. But that doesn’t work !!
So to use an ObjectOutputStream, a solution consists in retrieving the byte array of the ImageBuffer. To do that, look at thi

ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Now “fill” baos
ImageIO.write(screenShot, “jpg”, baos); // Here we want a JPEG file

Ok well. Now our baos is ok. We just have to call the method toByteArray() on our baos to retrieve a byte array :) And this array can be send through an OutputStream :)
That’s it :)

Enjoy doing Java :)

Follow

Get every new post delivered to your Inbox.