May 12, 2021 JSP
It's always hard to test/debug a JSP or servlet program. JSPs and servlets tend to involve a large number of client/server interactions, which can lead to errors and make it difficult to reproduce the error environment.
Here are some tips and suggestions to help you debug your program.
System.out.println() makes it easy to mark whether a piece of code has been executed. O f course, we can also print out a variety of values. In addition:
Next, the syntax for using System.out.println() is given:
System.out.println("Debugging message");
This is a simple example of using System.out.print():
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head><title>System.out.println</title></head> <body> <c:forEach var="counter" begin="1" end="10" step="1" > <c:out value="${counter-5}"/></br> <% System.out.println( "counter= " + pageContext.findAttribute("counter") ); %> </c:forEach> </body> </html>
Now, if you run the example above, it will produce the following results:
-4 -3 -2 -1 0 1 2 3 4 5
If you're using a Tomcat server, you'll be able to find .log in the stdout file in the logs directory:
counter=1 counter=2 counter=3 counter=4 counter=5 counter=6 counter=7 counter=8 counter=9 counter=10
This method allows variables and other information to be output into the system log to analyze and identify the underlying causes of the problem.
The J2SE log framework provides logging services for any class running in the JVM. So we can use this framework to record any information.
Let's rewrite the above code, using the logger API in JDK:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@page import="java.util.logging.Logger" %> <html> <head><title>Logger.info</title></head> <body> <% Logger logger=Logger.getLogger(this.getClass().getName());%> <c:forEach var="counter" begin="1" end="10" step="1" > <c:set var="myCount" value="${counter-5}" /> <c:out value="${myCount}"/></br> <% String message = "counter=" + pageContext.findAttribute("counter") + " myCount=" + pageContext.findAttribute("myCount"); logger.info( message ); %> </c:forEach> </body> </html>
It runs similar to the previous one, but it can get additional information output to the stdout .log file. H ere we use the info method in loger. Here's a snapshot .log the stdout file:
24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=1 myCount=-4 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=2 myCount=-3 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=3 myCount=-2 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=4 myCount=-1 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=5 myCount=0 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=6 myCount=1 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=7 myCount=2 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=8 myCount=3 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=9 myCount=4 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=10 myCount=5
Messages can be sent using a variety of priorities, using the sever(), warning(), info(), config (), fine (), finer (), finest() methods. The finest() method is used to record the best information, while the sever() method is used to record the most serious information.
Use the Log4J framework to record messages in different files, which are classified based on severity and importance.
NetBeans is a tree structure that is an open source Java integrated development environment that supports the development of stand-alone Java and web applications, as well as JSP debugging.
NetBeans supports several basic debugging functions:
Detailed information can be viewed in the NetBeans user manual.
You can use jdb commands for debugging in JSP and servlets, just like you would for a normal application.
Typically, we debug the sun.servlet.httpserver object directly to see how httpServer performs JSP/Servlets in response to HTTP requests. T his is very similar to debugging applets. The difference is that the applets program actually debugs the sun.applet.AppletViewer.
Most debuggers automatically ignore some details when debugging applets because they know how to debug applets. If you want to transfer debug objects to JSP, you need to do two things:
Once classpath is set up, start debugging sun.servlet.http://www.http-server. You can set a break point anywhere in the JSP file, as long as you like, and then use your browser to send a request to the server so that you can see that the program is parked at the break point.
Comments in the program in many ways to the program debugging play a certain role in helping. Comments can be used in many aspects of the debugger.
JSP uses Java annotations. If a BUG disappears, take a closer look at the code you just commented on and usually find out why.
Sometimes it is useful to view unprocessed HTTP requests and responses when JSP is not running as intended. If you're familiar with the structure of HTTP, you can look directly at request and response and see what's going on with these head modules.
Here are two more tips for debugging JSPs: