Getting Started with GlassFish in IntelliJ IDEA
IntelliJ IDEA
7.0.x include plugins
that provide support for configuring GlassFish.
This blog provides clear instructions on how to get started by
developing and deploying a JSP, Servlet and Web services using
GlassFish in IntelliJ. The instructions are using IntelliJ 7.0.3 Build
#7757 (with no additional plugins).
- Create a new project
- Clicking on "Create New Project" or "File", "New
Project". Take the default as shown below:

and click on "Next >".
- Enter the project name as "GlassFishRocks" and take all
defaults as shown:

and click on "Next >".
- Take another default for the source directory as shown:

and click on "Next >".
- For the first time use, JDK needs to be specified. Click
on "+" in top-left corner as shown here:

Take the default option of "JSDK" and specify the Home Directory as
shown:

Click on "OK" and then click on "Next >".
- Let's create a Web application. Select the list of
technologies as shown:

and finally (phew!) click on "Finish". The expanded project looks like:

- Create a GlassFish configuration
- Select "Run", "Edit Configurations" as shown:

- Click on "+" on top-left corner and select GlassFish as
shown below:

- Specify the location of GlassFish Application server at:

by clicking on "Configure" button and enter the values as shown:

and click on "OK". You can download and install GlassFish v2 UR2 from here.
- Enter the "Name" and select the "Server Domain" as shown:

and click on "OK".
- Deploy the Web application
- Click on the green button in the toolbar:

- Click
on the "Fix" button on the bottom and then click "Run". The recently
created Web module is selected to be deployed as shown:

- This starts the GlassFish v2 UR2 Application Server and
deploys the Web application showing the console as:

and also shows the default page at
"http://localhost:8080/GlassFishRocksWeb/". You can edit "index.jsp",
re-deploy the Web facet and refresh the page to see the updated message.
Notice, even though project's name is "GlassFishRocks", the application
context root is "GlassFishRocksWeb".
- Now lets create/deploy a new Servlet.
- Create a new project as described above and name it
"KillerServlet".
- Right-click on the project and select "New", "Servlet" as
shown:

- Enter the values as shown:

and click on "OK".
- The "Java EE: Structure" shows the project as:

- Double-click on "HelloServlet" (nested one) and add the
following fragment to "doGet" method:
java.io.PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet
NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet NewServlet at " +
request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
} |
NetBeans IDE
auto-generates this code for a Servlet ;-) And add the following to
"doPost" method:
|
doGet(request, response); |
- Double-click on "web.xml" and then select "Assembly
Descriptor" tab.
- Click on "+" in Servlet Mappings and specify the values
as:

- Deploy the project (as described above) and output from
Servlet is displayed at "http://localhost:8080/KillerServletWeb/hello".
Read more details in Creating
Java EE Apps and Servlets with IntelliJ IDEA.
Remember the weird context root, it's "KillerServletWeb" instead of
"KillerServlet". Now there may be a good reason to do so but nothing
obvious.
- Now lets create a simple Web service using the Metro Web
services stack (the stack baked into GlassFish)
- Create a new project with name "GlassFishWS" following
the instructions given above.
- Select the list of technologies as shown:

- The default generated Web service looks like:

- The default generated Web service uses light-weight
Endpoint
API to host the endpoint. Run the Web service by right-clicking in the
editor pane and selecting "Run" as shown or default shortcut of
Ctrl+Shift+F10:

- The WSDL is now available at
"http://localhost:9000/HelloWorld?wsdl".
- Right-click on the project and select "New", "Web Service
Client" as shown:

enter the value as "WSClient" and click on "OK".
- In the next dialog, enter the values as shown:

- The generated client code has some errors as shown:

Change the code to:
client.HelloWorld service = new
client.HelloWorldService().getHelloWorldPort();
//invoke business
method
System.out.println(service.sayHelloWorldFrom("Duke")); |
and run WSClient.main to see the result as:

Now you deployed a Metro Web service using light-weight Endpoint API.
The bundled plugin version is 0.9 build 2 and the steps are so
much cleaner from 0.7 version of the plugin.
Read more about Web
Services support in IntelliJ IDEA.
- Deploying this Web service on GlassFish is really simple.
- Create a new GlassFish configuration as explained above.
- Run
the project using this configuration and the Web service is now hosted
at "http://localhost:8080/GlassFishWSWeb/services/HelloWorld?wsdl".
- Generate a client using the steps described above.
Here are few issues filed:
- JEEAS-180
does not allow an application to be re-deployed to GlassFish and that's
why the examples above use different projects.
- JEEAS-181
asks for better integration of GlassFish logs in the IDE.
- JEEAS-182
require support for GlassFish v3 in the GlassFish plugin. Please help
by voting for this issue.
- WSVC-61
reports the errors generated in Web services client code
So whether you are using Eclipse, IntelliJ or NetBeans - you can easily
configure GlassFish and deploy your applications directly from within
the IDE. Here are some related links:
However of all the IDEs, NetBeans IDE still provides the most
comprehensive coverage
in terms of development and deployment of
Java
EE applications (JSP, Servles, Java Server Faces, SOAP-based
.NET 3.0-interoperable Web service, RESTful Web services, JPA, EJBs)
and
server
plug-ins (GlassFish, Tomcat, JBoss, WebLogic, WebSphere,
OC4J, SAP BusinessOne and JOnAS).
Technorati: glassfish
intellij
idea jsp servlets
metro
webservices
Posted
by Arun Gupta in webservices |

|

|

|

|

|

|
|
Picture at stage 8 is not correct enough, this is partial code that to be filled by the user (aka live template), if it is not completed by the user then the code looks like having errors
Posted by Maxim Mossienko on July 10, 2008 at 07:02 AM PDT #
The pictures shows the default generated Web services client code. http://www.jetbrains.net/jira/browse/WSVC-161 has been filed to track this issue. The correct client code is given right after the image anyway.
Posted by Arun Gupta on July 10, 2008 at 09:31 AM PDT #
It seems I am no clear enough, on stage 8 one need to return focus to the editor and finish live template (select particular web service class / method / parameters), there is no need to replace the code completely as you suggest.
Posted by Maxim Mossienko on July 10, 2008 at 09:57 AM PDT #
Maxim, Completing the live template is certainly an option. However the generated code template has few errors:
- ().() is a syntax error and needs to be fixed.
- For parameters, it would be nice to provide default values (for example "" for String) such that the user knows how to get started.
- Invoking service.sayHelloWorldFrom() is incorrect because this method exists on Port, not Service.
I consider it easier to replace the entire code instead of trying to "guess" the function names and parameter types.
Posted by Arun Gupta on July 10, 2008 at 10:04 AM PDT #
You will need no guess about method name, only to choose initial web service interface type, adequate method will be inserted automatically (which return port), please, try it.
I agree that when focus is moved away from the editor the things become confusing :)
Posted by Maxim Mossienko on July 10, 2008 at 10:14 AM PDT #
I think I'm more used to NetBeans which generates the ready-to-go template code for me :) I'll try your suggestion though.
Posted by Arun Gupta on July 10, 2008 at 10:17 AM PDT #
IDEA 7.0.3 will not properly debug against a LOCAL Glassfish instance for some reason. It refuses to properly connect to the JPDA port. I have only been able to debug a 'local' glassfish by setting up a remote glassfish configuration and using my local host name or 127.0.0.1 - I have filed the following bug against this issue:
http://www.jetbrains.net/jira/browse/JEEAS-143
The last comment in the bug indicates another ticket w/a solution, however, it does not work so just be aware of this and you will save yourself hours of frustration until it is fixed.
Posted by NBW on July 10, 2008 at 04:20 PM PDT #
NBW, thanks for the tip! I'll try it later.
Posted by Arun Gupta on July 11, 2008 at 04:05 PM PDT #
Posted by Arun Gupta's Blog on July 18, 2008 at 07:11 AM PDT #
I've followed the steps you've mentioned but cannot get a simple JSP or servlet to deploy using the Glassfish PlugIn. I'm using Glassfish3.0b29 with IntelliJ8.1.3 on WindowsXP - yes some of us still use it :-(
When I start the container all I see in the IntelliJ console is :
----------------------------------
cmd /c C:\glassfish-v3-ea-b29\glassfishv3\bin\asadmin.bat start-domain domain1
Name of the domain started: [domain1] and
its location: [C:\glassfish-v3-ea-b29\glassfishv3\glassfish\domains\domain1].
Admin port for the domain: [4848].
------------------------------------
and the output stays stuck. Can you please help / provide some suggestion on what maybe going on with IntelliJ or with Glassfish? I am a Glassfish newbie so please excuse any naive questions.
Posted by Ravi on June 30, 2009 at 09:51 AM PDT #