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
- Click Download Eclipse link to go to download page. Click related link in red square to download the correct version for your operating system.
- 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.
- 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
- 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.
- Open your Java EE eclipse neon, click Window —> Preferences.
- Click “Server —> Runtime Environment” in left panel. Then Click “Add” button in right panel.
- 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).
- 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.
- 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.
How to use Tomcat In Java Web Project
- Click “File —> New —> Dynamic Web Project” to create a new web application project in java EE eclipse IDE.
- Input project name and choose project save folder in popup dialog. Choose correct target runtime.
- 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.
- 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.
- Right click the java web application’s project name. Click Properties in popup menu.
- Click Server in left panel then choose “Tomcat v9.0 Server at localhost” in right panel.
- 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.
- 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.
Create index.jsp
- Right click WebContent folder.
- Click “New —> JSP File” in popup menu.
- Change the jsp file name to index.jsp. Click Next, click Finish.
- 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>
- Double click the beginning of each java code line to set or remove a break point.
- Right click project name, click “Debug as —> Debug on server” to start tomcat debugging.
- 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.
- 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.
2). Click “Window —> Web Browser” to choose default running browser for debugging.