JSP Debugging

nutmegactΛογισμικό & κατασκευή λογ/κού

10 Νοε 2012 (πριν από 4 χρόνια και 8 μήνες)

250 εμφανίσεις

1

Chapter 14:


JSP Debugging

Reference: Beginning JSP

2

Objectives


Debugging: Art or Science?


Debugging Techniques


Compile Time Bugs


Console Output


Log4J


Debugging with Tools


NetBeans 4.1 JSP Debugger

3

Debugging: Art or Science?


Pages 436
-
437



Science of Debugging


Observation


Program does not work as expected


Hypothesis


Guess what causes the unexpected behavior


Prediction


How a change in code or data might affect the observation


Test


Test the prediction by making the change and observing new
results

4

Compile Time Bugs


If JSP fails to compile


Servlet container often returns a message to the
browser explaining the problem


In NetBeans 4.1
, compile time bugs are caught when
JSP page is compiled


Example:


<%@taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/
bore
" %>


In Netbeans 4.1:


Build | Compile Example1.jsp shows:

org.apache.jasper.JasperException: The absolute uri:
http://java.sun.com/jsp/jstl/bore cannot be resolved in either
web.xml or the jar files deployed with this application

5

Debugging Scriptlets in JSP


Pages 439
-
441


In NetBeans 4.1:


Build | Compile Example2.jsp


Result: Cannot find symbol

symbol : class Random

location: class org.apache.jsp.Example2_jsp


Random r = new Random();


^


Solution: Add import statement

<%@page import="java.util.Random" %>

6

Instrument JSP Code


Pages 442
-
444


Using
System.out.println


Very basic, difficult to
disable, etc.


Run Example3.jsp and
Example4.jsp


Results logged under
Output

tab


Bundled Tomcat 5.5.7

7

Servlet Container Logging System


Pages 445
-
446


Using the
ServletContext.log()



No control which
messages to log


Run Example5.jsp

<% String message = …;


this.log( message );

%>


8

Logging with JDK Logger


Pages 447
-
454


Using the
Logger.info()

depending on the
logging level


Set the level in
<JAVA_HOME>/jre/li
b/logging.properties


globally configured


E.g.,
org.apache.jsp.level =
INFO

9

Logging with Log4j


Pages 454
-
460


Advantage:


Log4j JAR can be
deployed as part of
an application


not globally
configured


Set the level in
WEB
-
INF
/classes/log4j.prope
rties


10

Logging with Log4j and Tag Libraries
(Scriptless JSP)


Pages 460
-
464


Unlike previous approaches, does not require
scriptlets in JSP


E.g.,
http://localhost:8084/ch14/LogTags.jsp



11

Debugging with Tools


Using NetBeans 4.1


Set breakpoints


Examine variables


Step through code


Step Over, Step Into, Step Out, Continue,
Terminate


Remote debugging


Attach to port 8084 (default)


12

Next Steps


Try It Out sections of Textbook (Chapter
14)


Examples 1 to 7


LogTags.jsp


You can skip the rest of the examples and
exercises