X

Getting Started with GlassFish in IntelliJ IDEA

Guest Author


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="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/59e708936ccaf0e2ad4f7c907b35f103/intellij703_create_new_project.png">


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

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/c7e9bf916cb28aaf841764400e98b102/intellij703_new_project_name.png">


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

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/a224000666af4772c7f9754aea41365d/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="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/a737db8abdce5786e326917f4dd176a9/intellij703_new_project_addjdk.png">


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

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/1f4fa9eaa6f6de57e1586d8d918413e5/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="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/c4d2536103da96ceb465e5e30d9c9d90/intellij703_new_project_web_technology.png">


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

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/40b0cc7d8f2b46b1ae37809f6b75445e/intellij703_web_default_view.png">
  2. Create a GlassFish configuration
    1. Select "Run", "Edit Configurations" as shown:

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/fb5cf51da06385691b548bd185390085/intellij703_edit_configs.png">
    2. Click on "+" on top-left corner and select GlassFish as
      shown below:

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/3b9511391ed777cb86c0c9d5b6f6fe77/intellij703_add_glassfish_config.png">
    3. Specify the location of GlassFish Application server at:

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/d99310eaec6308d93a6ae1c70ef3d05b/intelli703_add_config_default.png">


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

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/a87682e1f9b1c09d65e036a474de3b77/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="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/24439a8efb3253fdb46c2d2fe0255be0/intellij703_glassfish_config.png">


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

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/373f70b2882a88a9c183c9ddb0a57e89/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="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/3b5bfe9865949f784976e43fcb548123/intellij703_deploy_ws.png">
    3. This starts the GlassFish v2 UR2 Application Server and
      deploys the Web application showing the console as:

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/2346cd368879c20321918693fe86a446/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="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/b59d76d105d8bdbd24d7f92e4b52c942/intellij703_new_servlet.png">
    3. Enter the values as shown:

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/c8a4132855e747ec77f9c14a3d8a5fd8/intellij703_new_servlet_values.png">


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

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/26e6f1803bed5b208f37da4fad192a1e/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="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/e94b2667e25f9e6dde06f362026689c2/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="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/0ad529cf0be278a8d99e84d080a7c80a/intellij703_new_project_technologies.png">
    3. The default generated Web service looks like:

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/f6d9ad77f08989c65a8af7b0868d95fc/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="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/448fecbd57eda46c40d9b3755eff5cf9/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="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/2a7f47f4bd7e32fa11bcf0a5119c69f9/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="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/15e4842db59c0636036bac3f1e6567f3/intellij703_ws_client.png">
    8. The generated client code has some errors as shown:

      src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/14d9abf74eda3fe67208b7ba8c7a3848/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="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/9b7c8a53d8d386997eac61ccb5940c10/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

Join the discussion

Comments ( 10 )
  • Maxim Mossienko Thursday, July 10, 2008

    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


  • Arun Gupta Thursday, July 10, 2008

    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.


  • Maxim Mossienko Thursday, July 10, 2008

    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.


  • Arun Gupta Thursday, July 10, 2008

    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.


  • Maxim Mossienko Thursday, July 10, 2008

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


  • Arun Gupta Thursday, July 10, 2008

    I think I'm more used to NetBeans which generates the ready-to-go template code for me :) I'll try your suggestion though.


  • NBW Thursday, July 10, 2008

    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.


  • Arun Gupta Friday, July 11, 2008

    NBW, thanks for the tip! I'll try it later.


  • Arun Gupta's Blog Friday, July 18, 2008
    [Trackback] I presented on GlassFish at Utah JUG yesterday,&nbsp;slides are available. The topic provided insight into GlassFish v2, the current production version, and GlassFish v3 - the upcoming&nbsp;modular, embeddable &amp; extensible version. There were close...
  • Ravi Tuesday, June 30, 2009

    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.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.