Wednesday, March 23, 2011

Debugging JavaServer Faces

While attempting to test some session handling code for JavaServer Faces, I ran into a Java Null Pointer Exception.  The code causing the error was written directly into the jsp file itself instead of in a backing bean.  The cause of the error is still eluding me as I did not yet delve into it; however I discovered that debugging jsp files is not as hard as it seems.  Initially I was stumped because the output from glassfish stated that the exception error occurred on line 82:  My JSP file only has 61 lines.  Being experienced with .NET it's easy to understand why the error numbers would differ.  Normally in .NET one would use codebehind and the error's line number would match your code's line number.  Similarly the normal way to code in JavaServer Faces is to ensure all your logic code is in a backing bean;  then the error's line number matches with the code's line number.

In any case I wanted to know the location of the error in my code  (JSF generates a .java code file from your JSP file when it is compiled.)  The only trick was to find the location of this generated file and the line number from the error matches the line number in the .java file.

Here is a sample of the code from my JSP page and the "translation" from the .java file.  The location of the .Java file may vary, but mine was located at: /home/<username>/.netbeans/6.9/config/GF3_62/domain1/generated/jsp/various_tests/org/apache/jsp/index_jsp.java

JSP code:
21
22
23
24
25
System.out.println("reached point 1");
FacesContext ctx = FacesContext.getCurrentInstance();
HttpSession mySession = (HttpSession) ctx.getExternalContext().getSession(false);
String s = mySession.getAttribute("test").toString();
System.out.println(s);

Java code:
80
81
82
83
84
System.out.println("reached point 1");
FacesContext ctx = FacesContext.getCurrentInstance();
HttpSession mySession = (HttpSession) ctx.getExternalContext().getSession(false);
String s = mySession.getAttribute("test").toString();
System.out.println(s);

Line 82 is highlighted in red and it's equivalent in the JSP file is line 23.

No comments:

Post a Comment