11

I know that in a servlet or in a normal Java file we can get the line number where the exception occurred using the printstacktrace method.

But because a JSP is converted to a servlet, I was not able get the exception line number even by using printstacktrace method. Is there any way to get the line number (where exception occurred) of the JSP file when an exception occurs.

My JSP file:

Line Number:
17:        <%
18:            try {
19:                int a[] = new int[1];
20:                System.out.println(a[3]);
21:            } catch (Exception e) {
22:                e.printStackTrace();
23:            }
24:        %>

Here the exception is actually happening at line number 20, but when I run it, it is showing an exception at line number 77, which I don't even have in my file.

SEVERE:   java.lang.ArrayIndexOutOfBoundsException: 3
at org.apache.jsp.index_jsp._jspService(index_jsp.java:77)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:253)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:428)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)

Because it is converting the jsp to a servlet, it is showing the line number as 77 instead of 20, but I just want it to print the line no as 20 (where exception was occurred) instead of 77, because in a larger file, I may find it difficult to locate where the exception was happening.

kc2001
  • 5,008
  • 4
  • 51
  • 92
Prakash
  • 693
  • 5
  • 17

4 Answers4

11

Every JSP is converted to a servlet on runtime, which means you should look for the generated servlet code. In Tomcat it is in the work/ folder, but to make sure, search for the index.jsp.java file in your servlet container's folder.

V G
  • 18,822
  • 6
  • 51
  • 89
  • Search manually for that file in servlet containers folder. You can do that, don't you? – V G Mar 25 '14 at 11:03
  • yes thanks i found that file it was in the location '**glassfish-4.0\glassfish\domains\domain1\generated\jsp\Struts2Sample\org\apache\jsp\index_jsp.java**' and line number also correct Thanks @Andrei I – Prakash Mar 25 '14 at 11:06
  • k do you have any idea about debugging web application in netbeans @Andrei I – Prakash Mar 25 '14 at 11:12
  • @Prakash I do not use Netbeans, but you could try asking another question, may be there will be some guys who can help you. – V G Mar 25 '14 at 11:19
  • For Websphere it was "\IBM\WebSphere\AppServer\profiles\AppSrv03\temp\Node04\server1\ear\war\WEB-INF\jsp\" – rlshep May 22 '15 at 15:46
  • 1
    @PhilipRego For IntelliJ on Mac at least, look to `~/Library/Caches/IntelliJIdea2019.3/tomcat/Unnamed_WebName/work/Catalina/localhost/ROOT/org/apache/jsp/WEB_002dINF/jsp/MyPage_jsp.java` -- the path will vary depending on your. version of IntelliJ, WAR context root (mine is in /), and this one is a JSP inside my WEB-INF directory – Piran Apr 22 '20 at 09:52
  • This worked for me, I found the error line in compiled jsp, it is in target folder. – huuthang Jun 16 '20 at 03:44
1

This and other problems made me reduce the Java code in JSP pages to the absolute minimum. I create helper objects for everything I need to do in a JSP page which contains the meat of the Java code (which means I can unit test this code as well).

The JSP then only contains the bare minimum:

<%Helper tool = new Helper();%>

<%=tool.foo()%>
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Did you meant that create a separate method in a class and just call that method in jsp – Prakash Mar 25 '14 at 10:53
  • but if we have to get some values(say 10) from the request or session then it makes it a bit difficult though.because we have to call 10 methods from jsp? – Prakash Mar 25 '14 at 11:17
  • Yes, my method isn't for free. You have to design your JSPs accordingly. In several instances, I'm using tools like [rendersnake](https://code.google.com/p/rendersnake/) so I can generate HTML from Java without going back and forth all the time. – Aaron Digulla Mar 25 '14 at 12:58
1

Sometimes it's helpful to calculate the offset of the source line of the JSP file. E.g.:

System.out.println("Line 9: line number offset: " + (new Exception().getStackTrace()[0].getLineNumber() - 9));

Of course you need to place this block after each inlcude or JSP comment since they cause the offset to change.

René
  • 71
  • 7
  • This way is hacky and bad coding practice, you need to maintain the offset If anyone updates the JSP file, please follow the accepted answer. The exception in the log will indicate the error line in the compiled jsp file – huuthang Jun 16 '20 at 03:43
0

This answer actually explains how to debug JSPs specifically: JSP debugging in IntelliJ IDEA

Community
  • 1
  • 1
Maheshbabu Jammula
  • 357
  • 1
  • 2
  • 11