Thursday December 22, 2005
Insert Witty Irony Herevince kraemer's Weblog
All
|
5 in 5
|
Compile Time
|
Ease of Evolution
|
General
|
GlassFish
|
Gotchas
|
Java
|
Music
|
NetBeans
|
Sailfin
See the Java Console on Mac OS X While trying out GlassFish Project's Java EE 5 implementation on Mac OS X 10.4, I ran into hiccup with application clients. I soon discovered that other folks had hit the same issue, and some hints at a solution. Note: the response from the developer, tjquinn, is one of the most complete responses I have ever read in a bug report. All I needed to do was translate his response into Apple.
After some poking around, I found the 'Java Preferences' utility in /Applications/Utilities/Java/J2SE 5.0. This utility let you set the proper flags to open the Java Console.
![]() The Java Preferences interface: Java Console radio button visible Once I set it up, I was able to see the output of batch oriented application clients that get launched by Java Web Start. (2005-12-22 11:31:52.0) Permalink Web Application/EJB 3.0 "Hello, <USER>!" This entry covers the "usual" first Java EE 5 application: Hello, World. The first thing you need to do is create a new project that will be deployed onto the GlassFish instance that you registered earlier.
There are a couple different approaches you could take here. I recommend that you create an Enterprise Application, and let the wizard create the Web Application subproject. Part of the reason for this is lets you be lazy later on. While you may like creating servlets by hand, I like to let the wizards do it. Since the Servlet wizard is associated with Web Application projects, using an approach that doesn't use the Web Application project will mean you need to do more work. One other thing to notice about the wizard is the fact that I have 'Set Source Level to 1.4' UNCHECKED. This will allow you to use annotation later on, so make sure you have it unchecked, too. After the wizard finishes, create a java library project. It is going to be the project where you do the EJB 3.0 devlopment.
The Web Application and Java Library project needs to have access to the javaee.jar file, which is in <GlassFish-Install>/lib. It has the definition of the various Java EE 5 annotations that you'll use later on. You use the Project Properties dialog to add this jar file to the Web Application and Java Library projects.
The two Project Properties dialogs are very similar. You will see an additional column in the Web Application project's properties dialog. Make sure 'Package' is unchecked when you add javaee.jar to the Web Application project. Otherwise, the jar will be packaged in your web application's WEB-INF/lib directory, which is redundant.... You need to add the Java Library project to the compile properties of the Web Application, and the packaging properties of the Enterprise Application project. (Don't forget to uncheck the 'Package' attribute when you add the library project to the Web Application project).
You need to add the Java Library that will implement your EJB 3.0 beans to the application.xml manually. Double clicking on the application.xml node, shown below, will open the file in an XML editor. It is easy to add the following: <module><ejb>Greeter.jar</ejb></module>
![]() Expanded Enterprise Application Project: application.xml visible At this point the infrastructure is in place and it is time to start writing Java EE 5 code. The coding required to create an EJB 3.0 bean is wonderfully concise (compared to EJB 2.1). First, we define the interface that says what a bean can do. This the text of the interface that I created.
package beans;
import javax.ejb.Local;
@Local public interface FriendlyGreeting {
public String returnFriendlyGreeting(String name);
}
After defining the interface we need to create the implementation. That is relatively straight-forward, too.
package impl;
import beans.FriendlyGreeting;
import javax.ejb.Local;
import javax.ejb.Stateless;
@Stateless public class GreetingImpl implements FriendlyGreeting {
public String returnFriendlyGreeting(String name) {
return "Well, good to see you again, "+name+"!";
}
}
The one exception would probably be in the development of entity beans, which still require a lot of meta-data. Once the bean is ready, it is time to call it. In my example, I am going to create a servlet to do that. Other folks have demonstrated other methods for calling your EJB 3.0 bean from a Web Application. The easy way to create the servlet is to use the New item from the Web Application project's right-click menu. Here is the text of my servlet.
package lets;
import beans.FriendlyGreeting;
import java.io.*;
import java.net.*;
import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;
public class EJB3Greeting extends HttpServlet {
@EJB FriendlyGreeting bean;
/** Processes requests for both HTTP
At this point we are almost done. There is still one thing that needs to be changed and NetBeans has been finicky lately when I do this edit. You need to change the text of the web.xml, so that it refers to a Java EE 5 schema, instead of the J2EE 1.4 schema. The thing to do is replace the initial web-app element (which will look something like:
<web-app version="2.4"
xmlns="http://java.sun. com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
with somehting like this
<web-app version="2.5"
xmlns="http://java.sun. com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
The easiest way to make this stick is to copy the text above and paste it over the "old" web-app element in the web.xml file.
At this point you are ready to run the application. The thing to do is make sure that the project SampleOne is the Main Project. You can use the 'Set Main Project' item from the Enterprise Application right click menu to make it so. If it is the main project, then you can use the F6 key, to trigger the 'Run Main Project' item of the Run menu.
The IDE will start the server, deploy the application and open the browser. It will trigger the default welcome-file, index.jsp. If you append "/EJB3Greeting?name=foobar" to the URL, you will see this:
After going through this initial exercise, I would say that the Java EE 5 has done a good job of simplifying 'Hello, World!'. Java EE 5 covers a lot more technologies that what I've used here. I will look at some of these other technologies and see wheher they have the improved their ease of development. (2005-12-22 07:07:23.0) Permalink |
Calendar
RSS Feeds
All /5 in 5 /Compile Time /Ease of Evolution /General /GlassFish /Gotchas /Java /Music /NetBeans /Sailfin About Me![]() Short Bio SearchLinks
Navigation
ReferersToday's Page Hits: 251 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||