Skip to main content

Getting Started with GlassFish in IntelliJ IDEA

Posted by arungupta on July 10, 2008 at 6:03 AM PDT



IntelliJ IDEA
7.0.x
include href="http://www.jetbrains.com/idea/features/application_server.html">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).

  1. Create a new project
    1. Clicking on "Create New Project" or "File", "New
      Project". Take the default as shown below:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-create-new-project.png">



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



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-new-project-name.png">



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



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-new-project-src.png">



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



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-new-project-addjdk.png">



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



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-new-project-jdkhome.png">



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



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-new-project-web-technology.png">



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



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-web-default-view.png">
  2. Create a GlassFish configuration
    1. Select "Run", "Edit Configurations" as shown:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-edit-configs.png">
    2. Click on "+" on top-left corner and select GlassFish as
      shown below:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-add-glassfish-config.png">
    3. Specify the location of GlassFish Application server at:



      src="http://blogs.sun.com/arungupta/resource/images/intelli703-add-config-default.png">



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



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-glassfish-path.png">



      and click on "OK". You can download and install GlassFish v2 UR2 from href="https://glassfish.dev.java.net/downloads/v2ur2-b04.html">here.
    4. Enter the "Name" and select the "Server Domain" as shown:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-glassfish-config.png">



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



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-run-config.png">
    2. Click
      on the "Fix" button on the bottom and then click "Run". The recently
      created Web module is selected to be deployed as shown:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-deploy-ws.png">
    3. This starts the GlassFish v2 UR2 Application Server and
      deploys the Web application showing the console as:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-glassfish-console.png">



      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".
  4. Now lets create/deploy a new Servlet.
    1. Create a new project as described above and name it
      "KillerServlet".
    2. Right-click on the project and select "New", "Servlet" as
      shown:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-new-servlet.png">
    3. Enter the values as shown:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-new-servlet-values.png">



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



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-servlet-javaee-structure-view.png">
    5. Double-click on "HelloServlet" (nested one) and add the
      following fragment to "doGet" method:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
             
      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:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
           
       doGet(request, response);
    6. Double-click on "web.xml" and then select "Assembly
      Descriptor" tab.
    7. Click on "+" in Servlet Mappings and specify the values
      as:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-servlet-mappings.png">
    8. Deploy the project (as described above) and output from
      Servlet is displayed at "http://localhost:8080/KillerServletWeb/hello".
      Read more details in href="http://www.jetbrains.com/idea/docs/Creating_Java_EE_Applications_and_Servlets_with_IntelliJ_IDEA.pdf">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.
  5. Now lets create a simple Web service using the href="http://metro.dev.java.net">Metro Web
    services stack (the stack baked into GlassFish)
    1. Create a new project with name "GlassFishWS" following
      the instructions given above.
    2. Select the list of technologies as shown:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-new-project-technologies.png">
    3. The default generated Web service looks like:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-ws-default-view.png">
    4. 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:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-run-ws.png">
    5. The WSDL is now available at
      "http://localhost:9000/HelloWorld?wsdl".
    6. Right-click on the project and select "New", "Web Service
      Client" as shown:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-new-ws-client.png">



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



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-ws-client.png">
    8. The generated client code has some errors as shown:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-generated-client.png">



      Change the code to:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
           
      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:



      src="http://blogs.sun.com/arungupta/resource/images/intellij703-ws-result.png">



      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 href="http://blogs.sun.com/arungupta/entry/web_services_in_intellijidea_using">so
      much cleaner from 0.7 version of the plugin.



      Read more about href="http://www.jetbrains.com/idea/features/java_web_services.html">Web
      Services support in IntelliJ IDEA.

    9. Deploying this Web service on GlassFish is really simple.
      1. Create a new GlassFish configuration as explained above.
      2. Run
        the project using this configuration and the Web service is now hosted
        at "http://localhost:8080/GlassFishWSWeb/services/HelloWorld?wsdl".
      3. 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:

  • href="http://weblogs.javahispano.org/lasterra/entry/glassfish_debug_with_intellij_idea">Debugging
    Apps on GlassFish using IntelliJ
  • href="http://blogs.sun.com/arungupta/entry/glassfish_on_eclipse_ganymede">GlassFish
    on Eclipse Ganymede
  • href="http://blogs.sun.com/arungupta/entry/netbeans_6_5_m1_glassfish">NetBeans
    6.5 M1: GlassFish v3 + Rails
  • href="http://blogs.sun.com/arungupta/entry/screencast_24_getting_started_with">Screencast
    #24: Getting Started with GlassFish v3 TP2
  • href="http://www.netbeans.org/community/releases/61/nb-mysql-bundle-install.html">NetBeans
    IDE and GlassFish
  • GlassFish
    Plugins

However of all the IDEs, NetBeans IDE still provides the most
comprehensive coverage
in terms of development and deployment of href="http://www.netbeans.org/kb/trails/java-ee.html">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: href="http://technorati.com/tag/glassfish">glassfish
intellij
idea href="http://technorati.com/tag/jsp">jsp href="http://technorati.com/tag/servlets">servlets
metro
webservices

Related Topics >>

Comments

harikumarsandhya, http://developers.sun.com/appserver/reference/techart/ws_mgmt.html provide details about Web services monitoring and management in GlassFish. I've asked the author of article to respond to your request as well. dxxvi, GlassFish is supported in NetBeans, Eclipse and IntelliJ - all 3. GlassFish plugin in IntelliJ does not support v3 yet. I filed an issue for that at: http://www.jetbrains.net/jira/browse/JEEAS-182 Please help by voting for this issue. http://www.jetbrains.com/idea/features/java_ejb.html talks about EJB 3.0 support, I may be able to look into later. http://weblogs.javahispano.org/lasterra/entry/glassfish_debug_with_intel... shows how to debug GlassFish using IntelliJ.

If a web application has servlets and jsps only, I guess nobody will choose Glassfish, but they'll choose Tomcat or some servlet container. So it will be helpful (at least for me) if you blog about developing an application using EJB3 in Intellij IDEA and running / debugging that app in GlassFish. I hope that EJB3 app will have separate modules for a war, jpa and session beans. It would be great if you can cover both IDEA 7 and 8 (because Idea 8 supports Seam, JavaScript debugging, SQL, Flex, JSF ... better than Idea 7). And it would be more great if you cover both Glassfish v2 and v3. I know that I request a lot without paying you anything. I cannot afford paying you but I hope somebody can so that you can blog about those wonderful things :) Best regards.

Long live Intellij Idea and Glassfish esp. when JBoss AS 5 is delayed until ... God know when.

can u suggest me how to monitor the web service using glassfish admin console ? I have already set the monitor level to high for the required web service, but I am not able to view the message trace. I am getting the statistics but the message traces are not available