2014-03-25 59 views
6

我知道在servlet或普通java文件中發生異常時,我們可以使用printstacktrace方法獲取行號(在該文件中發生異常)。如何在jsp頁面發生異常時獲取行號

但在JSP,因爲它是將其轉換成servlet中我沒能即使使用的printStackTrace得到異常的行號method.is有什麼辦法獲得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.but我只是想它打印無20(其中異常被髮生),因爲在insted的77的線更大的文件我也發現自己很難找到自己,是例外是發生

+0

arent u using eclipse? –

+0

使用netbeans.but在index.jsp.java中顯示異常,如** line:28只顯示jsp文件的行號。 – Prakash

+0

它正在顯示第28行.. –

回答

7

每個JSP被轉換爲上運行一個servlet,這意味着你應該尋找生成servlet代碼。在Tomcat中,它位於work/文件夾中,但要確保在您的servlet容器的文件夾中搜索index.jsp.java文件。

+0

我使用glassfish任何想法.. – Prakash

+0

在servlet容器文件夾中手動搜索該文件。你可以那樣做,不是嗎? –

+0

是的,謝謝我發現該文件位於'** glassfish-4.0 \ glassfish \ domains \ domain1 \ generated \ jsp \ Struts2Sample \ org \ apache \ jsp \ index_jsp.java **'並且行號也正確謝謝@安德烈我 – Prakash

1

這和其他問題使我減少在JSP頁面中的Java代碼絕對最小值。我爲包含Java代碼肉的JSP頁面(這意味着我可以單元測試這些代碼)創建幫助對象。

的JSP那麼只包含了最低限度:

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

<%=tool.foo()%> 
+0

你是否意味着在類中創建一個單獨的方法,並在jsp中調用該方法 – Prakash

+0

是的。我在JSP中幾乎沒有內聯Java代碼。 –

+0

聽起來不錯。但它是唯一的方法嗎? – Prakash

1

有時計算JSP文件的源代碼行的偏移量會有幫助。例如: -

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

當然,你需要的,因爲它們導致偏置改變放置此塊之後的每個inlcude或JSP評論。

相關問題