Debugging JSPs using Eclipse WebTools

As a general rule, I don’t like to have too much Java code in Java ServerPages (JSP) files. In fact, I’d prefer to have no Java code at all in a JSP. I have two basic reasons (and a collection of less basic reasons)… First, Java code in a JSP tends to be used to provide business logic; business logic doesn’t belong in the view layer and should instead by tucked somewhere behind a servlet. Second, JSPs are intended to be modified by content editors who–despite being wizards with HTML, JavaScript, AJAX, and whatnot–are blissfully unknowledgeable about Java (you don’t want these people to have the opportunity to change your Java code). That discussion aside, let’s just say that you have a very good reason to have a bunch of Java code in your JSP…

Before you can debug JSPs, you need to install the Eclipse WebTools. You can do this through the Callisto Update Site (click here to what a demonstration), or you can just download an all-in-one (Eclipse + everything you need for web development) here.

With WebTools installed, you can add one or more breakpoints by opening an editor on the JSP file, and double-clicking in the left-hand column. A breakpoint icon will appear as shown below.

Now, you need only start the server in debug mode. You can do this by right-clicking in the JSP editor or on the JSP file in the package explorer and selecting “Debug As > Debug on Server”, or by clicking the “Start in Debug Mode” icon (little picture of a bug) on the “Servers” view (shown below).

When execution reaches the breakpoint, Eclipse will drop you into the Debug perspective where you can hop, skip, jump, and inspect variables to your heart’s content.

This entry was posted in Uncategorized. Bookmark the permalink.

8 Responses to Debugging JSPs using Eclipse WebTools

  1. Erik says:

    I am horribly unimpressed with the JSP editor that comes with WTP. Do you have another recommendation?Erik

  2. Wayne says:

    How about MyEclipse, Lomboz, or Rational Application Developer?

  3. Julia says:

    It’s hard to admit this, considering how long I’ve been working with Java and Java-related technologies, but I had either forgotten or just never realized that yes, you CAN debug JSPs! I need to open my mind up a little, so the next time I think something along the lines of “man, I wish you could debug JSPs”, I instead start thinking it’s probably a problem that someone else has already solved… and look for it, or solve it myself. Sheesh.I rarely put any java code in my JSPs, however I’m mentoring a bunch of coders, some of who have put java code in their JSPs “for now”. Yet, some of this code gets buggy…. and causes NullPointerExceptions and then we both start scratching our heads.I looked up your post at work today, and added a breakpoint… I found the problem in seconds, and wouldn’t have thought to look there, because it wasn’t my code! I’d send you a Thanks! award, but you’re external now. So, tell me if and how I can donate to this cause or if I’ll just have to wait til next January. and keep up the superb Eclipse tips.Thanks a ton, Wayne.

  4. shashi says:

    Maybe i’m joining a bit late, but still to my curiosity i was thinking can we also debug javascript code inside a jsp file. While trying my hands on debugging a JSP file, i found that i can set breakpoints on java code (inside scriptlets) but i can’t set breakpoints on javascript code. Is That Possible At All??

    • waynebeaton says:

      Pretty late… 🙂

      I don’t know the answer. I suspect that the challenge here is that JavaScript is running in the browser while the JSP code is running on the server. I don’t know if anybody (other than, perhaps, GWT) have decent integration between the two runtime environments. Somebody from the JSDT or VJet project may have an answer:

      • shashi says:

        Thanks for the response. I see your point that JSP code is running on the server and javascript on browser. A good integration between two would not be that easy and obviously the possibility of dubugging a JSP code directly, seems too little feasible. Although FireBug does a brilliant job for me (and seems sufficient as well), there might be some alternative to set the breakpoints directly on your JSP file and test the flow (the traditional way). I might check this on your suggested forum. Would share, if found something useful. 🙂

  5. Divya malhotra says:

    I am using the eclipse indigo, wtp is already installed in my eclipse but still i am unable to debug the jsp page. Whenever i put the breakpoint in the jsp , the debugger never stops at those debug points.Server is jboss 5.1.

    Could anyone please explain the reason.?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s