views:

690

answers:

3

Here is the stacktrace:

...
org.apache.jsp.showcustomer_jsp._jspService(showcustomer_jsp.java:128)
org.apache.jasper.runtime.HttpJspBase.service(Unknown Source)

This is what I do:

  1. Get the line number from the stacktrace, in this case 128.
  2. Find the showcustomer_jsp.java file (and it isn't exactly obvious to look in /var/run/tomcat-6/Catalina/localhost/_/org/apache/jsp).
  3. Open it and go to line 128.
  4. Now, search the .jsp file for the whatever you found on line 128 in the _jsp.java file.
  5. Boom! You're done!

Please, is there a simpler way to do this?

A: 

If you have access to the Throwable, you can use its getStackTrace() method which will return StackTraceElements which, in turn, will give you access to the exact line number.

There is also a getFileName() method but I don't know what it returns for JSPs (nothing useful, I suspect).

Xr
+1  A: 

I don't think you can. The JSP file is compiled into a servlet, and is not run directly. As the exception is thrown from this servlet, the line you have in the stack trace is the one from the class. The original line in the JSP is lost at this point.

Usually it is best to avoid writing code or throwing exceptions from a jsp, and encapsulate your logic in servlets and JSP tags, and use JSTL for control flow (if, forEach, etc.)

David Rabinowitz
WebLogic had a really nice feature in pre-WL10 versions: it would write a comment into the generated Java file showing the source JSP line number. Even if your logic is encapsulated in a taglib, it's nice to see which invocation threw.
kdgregory
And a quick look at the Jasper sources seems to indicate that it processes JSP files as a token stream, without knowledge of line numbers. So the answer is no.
kdgregory
The IDE has all the data to compute the original line number. Perhaps someone could write an IntelliJ plugin that does the job.
Arne Evertsson
About JSTL: I'm not a fan of writing code as XML. It breaks the KISS principle.
Arne Evertsson
You can use other templating engines, such as velocity or freemarker
David Rabinowitz
A: 

I have found this page on Eclipse WTP FAQ which explain how to configure Eclipse so that you can go to the generated java code clicking on the stacktrace.

Johngeek