Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the line number if exception occurs in jsp page

Tags:

java

jsp

servlets

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.

like image 320
Prakash Avatar asked Mar 25 '14 10:03

Prakash


People also ask

How can we handle the exceptions in JSP?

There are two ways of handling exceptions in JSP. They are: By errorPage and isErrorPage attributes of page directive.

How do I display error messages in the same JSP page?

To create a JSP error page, we need to set page directive attribute isErrorPage value to true, then we can access exception jsp implicit object in the JSP and use it to send customized error message to the client.

What happens if an exception is thrown from a JSP?

JSP gives you an option to specify Error Page for each JSP. Whenever the page throws an exception, the JSP container automatically invokes the error page.

How to handle exceptions in JSP?

There are two ways of handling exceptions in JSP. They are: The page directive in JSP provides two attributes to be used in exception handling. They’re: errorPage: Used to site which page to be displayed when exception occurred. isErrorPage: Used to mark a page as an error page where exceptions are displayed.

How to handle errors in JSP page?

The pages where may occur exception, define the errorPage attribute of page directive, as in the process.jsp page. This approach is better because you don't need to specify the errorPage attribute in each jsp page. Specifying the single entry in the web.xml file will handle the exception.

How to define the page where may occur exception?

The pages where may occur exception, define the errorPage attribute of page directive, as in the process.jsp page. This approach is better because you don't need to specify the errorPage attribute in each jsp page.

When to throw exceptions in JavaScript?

There might be some scenarios that we need to throw exceptions in JavaScript, and get the line number, and the call stack, specially when we are developing a feature that will be used by other developers.


3 Answers

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.

like image 179
V G Avatar answered Oct 21 '22 23:10

V G


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()%>
like image 39
Aaron Digulla Avatar answered Oct 22 '22 00:10

Aaron Digulla


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.

like image 40
René Avatar answered Oct 21 '22 22:10

René