One of my reader was struggling with debugging a Java web application, containing Servlet and JSP in Eclipse with Tomcat. While helping him, I realized that I have not written anything about debugging a Java web application in Eclipse yet. Even though, I have wrote articles about debugging tips and remote debugging in Eclipse, I have never touched the topic of debugging Servlet and JSP in Eclipse or any other IDE, which are two pillars of Java web applications. To fill that gap, I am starting a series of articles where we'll first learn how to setup Eclipse with Tomcat to debug Java web application and then learn some tips and tricks to effective debug Servlet and JSP.
Eclipse allows you to debug a Java web application on a Tomcat configured and running on Eclipse, as well as a Tomcat running on remote server.
You can use all the steps given in my article about remote debugging Java application in Eclipse to debug a remote tomcat, after all Tomcat is a Java application.
All you need to know is that on which port it is listening to debug it. In general, it listen on port 8080 for HTTP traffic.
Can we debug JSP file in Eclipse?
Many of you might be confused whether you can debug JSP or not because it's not really a Java code. Some of you might have tried it before and faced problem that it doesn't allow you to setup a breakpoint. Well, that's true but not completely true.
You can both setup breakpoint in JSP as well as debug them locally as well as remotely running Tomcat server. Since JSP file contains HTML tags as well as JSP tag, if you try to put the breakpoint on HTML tags, Eclipse will not allow you to do that. Instead it will bookmark that line.
In order to setup breakpoint in JSP page, you need to put the breakpoint on the line where we have JSP code e.g. In following JSP page, the breakpoint can be placed at <jsp:forward> tag, as that's the JSP code, rest of them are HTML.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>Hello world </h1> <input type="checkbox" id="isAgeSelected"/> <div id="txtAge" style="display:none">Age is something</div> <jsp:forward page="header.jsp"></jsp:forward> </body> </html>
Some of you might think, why would you debug JSP page? Well, you may want to see the flow or you may want to check the values of implicit objects e.g. request, response, session, page, pageContext etc. I often debug JSP to see the request parameters and headers, which are key piece of information while processing JSP requests.
Can we debug Servlet in Eclipse?
Well, of course yes. Servlet is just a normal Java class when it comes to debugging. You can setup a line breakpoint on any line of your Servlet and debug it on Eclipse.
Normally, I put breakpoints on methods like doGet() and doPost() which are starting point of processing in Servlet. The doGet() method is called when a GET request is hit to your Servlet and a doPost() method is called when a POST request comes to your Servlet.
By putting a line breakpoint in these methods, you can easily see the content of HttpServletRequest and HttpServletResponse.
Now that you understand both JSP and Servlet can be debugged in Eclipse, it's time to setup the Tomcat in Eclipse for debugging.
Here is our Servlet class which we'll use for debugging:
public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String userAgent = req.getHeader("user-agent"); String clientBrowser = "Not known!"; if (userAgent != null) clientBrowser = userAgent; req.setAttribute("client.browser", clientBrowser); req.getRequestDispatcher("/hello.jsp").include(req, resp); req.getRequestDispatcher("/footer.jsp").include(req, resp); } }
Setup Tomcat for Debugging in Eclipse
As I told you in opening paragraph that you can debug a Java web application locally in Eclipse or remote debug it if the Server is running on a remote Linux machine.
For simplicity and ease of development I always setup a local Tomcat in Eclipse and mostly run my web application on that Tomcat using option "Run AS -> Run on Server".
Similarly you can also debug Java application on that local tomcat but first thing first, you need to attach Tomcat on Eclipse.
Here are steps to attach Tomcat on Eclipse:
Debugging Java Web Application in Eclipse
Now that your Tomcat is attached into Eclipse as Server, it's time to debug your Java Web application. In order to debug you can set up a breakpoint in any JSP or Servlet.
This is line where JVM will stop after starting the Tomcat server and executing your Servlet and JSP. At this point, you can then navigate your code step by step, check the values of various local and instance variables and see which thread is running your code.
Once you setup the breakpoint, just right click on your JSP page or Servlet class and choose the option "Debug As -> Debug on Server" as shown below:
Once you choose this option, Eclipse will first start the attached Tomcat server and deploy your application on that. Once application is deployed and loaded by Tomcat, it will stop at the line you put the breakpoint. If that line of code is not executed during startup then your application will not stop there e.g. a line which is only executed when an HTTP request is arrived.
Once JVM stops at your breakpoint, you can debug your Java web application by using debugging buttons like Step Into, Step Over etc. You can also see values of variables in Variables window as shown below:
Once you are done with the debugging you can stop the Tomcat Server by clicking on the red stop button on console as shown below:
That's all about how to debug a Java Web application in Eclipse with Tomcat. As I told you can setup breakpoint on both JSP and Servlet page and debug them as you debug a normal Java application.
I also suggest to attach Tomcat into your Eclipse IDE for ease of development and debugging but if you have to do the live debugging e.g. Tomcat running on remote machine you can always use the eclipse remote debugging steps I have shared earlier.
You can even debug a Tomcat running on your localhost but not under Eclipse by following those steps, just replace the remote host with localhost. If Tomcat is not attached to Eclipse then it is just a remote Java process for it and you can use remote debugging to debug that.
Other Java Eclipse Articles and Tutorials you May like
- 30 Useful Eclipse Shortcuts for Java Developers (list)
- 10 Eclipse debugging tips Java developer should know? (see here)
- How to attach source code for JAR file in Eclipse? (guide)
- Eclipse shortcut to print System.out.println statements? (shortcut)
- How to increase console buffer size in Eclipse? (steps)
- How to use spaces instead of tabs in Eclipse? (guide)
- How to remote debug Java application in Eclipse? (tutorial)
- How to create an executable JAR file from Eclipse? (example)
- 3 Books to Learn Eclipse IDE for Java developers (list)
- How to Increase Heap Size of Java Program running in Eclipse? (guide)
Thanks for reading this article so far. If you find this tutorial useful then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.
And lastly one question for you? Which one is your favorite IDE for Java development? Eclipse, NetBeans, IntelliJ IDEA, VS code or anything else?
1 comment :
Who is using JSP and Servlet now? We are now moved to React.js and all our frontend is client-side on JavaScript.
Post a Comment