如果在jsp页面中发生异常,如何获取行号

时间:2014-03-25 10:17:31

标签: java jsp servlets

我知道在servlet或普通的Java文件中,我们可以使用printstacktrace方法获取发生异常的行号。

但是因为JSP被转换为servlet,所以即使使用printstacktrace方法也无法获得异常行号。发生异常时,有没有办法获取JSP文件的行号(发生异常)

我的JSP文件:

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:        %>

这里的异常实际上发生在第20行,但是当我运行它时,它在第77行显示一个异常,我甚至没有在我的文件中。

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)

因为它正在将jsp转换为servlet,它将行号显示为77而不是20,但我只是希望它将行号打印为20(发生异常的地方)而不是77,因为在更大的文件,我可能会发现很难找到异常发生的位置。

4 个答案:

答案 0 :(得分:9)

每个JSP都在运行时转换为servlet,这意味着您应该查找生成的servlet代码。在Tomcat中,它位于work/文件夹中,但要确保在servlet容器的文件夹中搜索index.jsp.java文件。

答案 1 :(得分:1)

这个问题和其他问题使我将JSP页面中的Java代码减少到绝对最小值。我在JSP页面中创建了我需要做的所有事情的辅助对象,其中包含了Java代码的内容(这意味着我也可以对这段代码进行单元测试)。

然后JSP只包含最低限度:

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

<%=tool.foo()%>

答案 2 :(得分:1)

有时计算JSP文件源代码行的偏移量会有所帮助。 E.g:

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

当然,您需要在每个inlcude或JSP注释后放置此块,因为它们会导致偏移量发生变化。

答案 3 :(得分:0)

这个答案实际上解释了如何专门调试JSP:JSP debugging in IntelliJ IDEA