Debug Java EE Web Application In Tomcat Use Eclipse

One of the most useful skill you should take is deploy and debug Tomcat within your Java EE Integrated Development Environment. This skill can make it easy for you to find out the reason why your Java EE web application go wrong as well as debug and fix the problem quickly. In this article we will show you how to use Eclipse to setup, debug and deploy web applications in Tomcat step by step.

Steps to install Eclipse Java EE Neon

  1. Click Download Eclipse link to go to download page. Click related link in red square to download the correct version for your operating system.
    download eclipse java EE neon
  2. After download, unzip the zip file into a local folder. I unzip it to “C:\WorkSpace\dev2qa.com\Tool\eclipse-jee-neon-3-win32-x86_64\eclipse” in my windows 7.
    unzip eclipse java ee neon 3 to a local folder
  3. Then you can click eclipse.exe to start it up. You can also create a short cut for eclipse.exe in desktop.

Steps to setup Tomcat 9 in eclipse

  1. Click here to learn how to install Tomcat 9 in your platform if you do not know. You just need to install Tomcat with command-line version.
  2. Open your Java EE eclipse neon, click Window —> Preferences.
    install tomcat 9.0 in eclipse java EE neon
  3. Click “Server —> Runtime Environment” in left panel. Then Click “Add” button in right panel.
    add tomcat 9 in java EE neon eclipse server runtime environment
  4. Choose Apache —> Apache Tomcat v9.0 in popup dialog.
    Do not forget Check “Create a new local server” checkbox, this can create the Tomcat server in your eclipse development environment which will be used for error debug later. Then Click Next.
    Click “Browse…” button in next dialog to select your Tomcat 9.0 home directory (Where you unzip Tomcat 9.0 zip file to).
    browse tomcat9 home directory
  5. You can choose either Workbench default JRE or your installed JRE. Then click “Finish”.
    Now you can see your Tomcat is installed in server runtime environment list.
    Then click Ok to return to eclipse workbench.
    java EE eclipse neon server runtime environment list
  6. When you return to the workbench, you can see a Servers folder in Project Explorer in left panel. This is just the Server which you can used to debug Java EE web application code.
    tomcat server that setup in java ee eclipse ide
READ :   Check Valid Number In Java String

How to use Tomcat In Java Web Project

  1. Click “File —> New —> Dynamic Web Project” to create a new web application project in java EE eclipse IDE.
    create java EE web application project in eclipse
  2. Input project name and choose project save folder in popup dialog. Choose correct target runtime.
    input web application project name and choose web application save folder
  3. Click Next twice then go to below dialog, check “Generate web.xml deployment descriptor” checkbox, this can create the web.xml automatically in the web application project.
    check generate web.xml deployment descriptor checkbox
  4. Please note “Context root” and “Content directory” in above picture.
    Context root‘s value is your web application’s path name in page url. If it’s value is Dev2qaWebAppExample, then you need to use following url to access this web application. http://localhost:8080/Dev2qaWebAppExample/ .
    Content directory is the directory where your all web application’s components stored.
    You will see below picture after click “Finish” button.
    context root and content directory
  5. Right click the java web application’s project name. Click Properties in popup menu.
    right click java web application project name
  6. Click Server in left panel then choose “Tomcat v9.0 Server at localhost” in right panel.
    choose tomcat9.0 as the server of eclipse debug environment
  7. If you want to change web application’s context root, you can click “Web Project Settings” in left panel, and change it in right panel.
    change web application context root in eclipse properties dialog
  8. Click OK to save the settings.

How to debug code with Tomcat?

To demo debug process in eclipse with Tomcat, we need to create an index.jsp file, this file also included in the welcome file list.index.jsp in web.xml welcome file list

Create index.jsp

  1. Right click WebContent folder.
  2. Click “New —> JSP File” in popup menu.
    create jsp file in eclipse
  3. Change the jsp file name to index.jsp. Click Next, click Finish.
    change jsp file name to index.jsp
  4. Open index.jsp and write below code into it. These java code will get current time in a long integer variable and print it out in the page.
    <body>
    
    <%
    
    long currTime = System.currentTimeMillis();
    
    %>
    
    CurrentTime is <%=currTime %>
    
    </body>
  5. Double click the beginning of each java code line to set or remove a break point.
    set break point in jsp page
  6. Right click project name, click “Debug as —> Debug on server” to start tomcat debugging.
    start debugging web application in tomcat
  7. Then Tomcat started and the process stopped at the break point you just set. You can press F6 to “step over” or F5 to “step into” the java code or “Ctrl + F12” to stop the debugging.
  8. If you click F8 to resume the debug process, you will see an eclipse internal web browser opened and display the web application’s welcome file. You can change the default running web browser by below method.
    1). Click “Java EE” icon at right top conner.
    click Java EE icon in right top corner
    2). Click “Window —> Web Browser” to choose default running browser for debugging.
    choose other web browser as default running browser for debugging
(Visited 589 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *