Preface
Anyone who has experience in JSP writing has the experience of JSP debugging difficulties. Due to the lack of automated debugging tools, a large number of print statements have to be used in files when debugging JSP. This approach is a simple and effective method for locating errors within the scope of JSP, but it is a bit inadequate to quickly and effectively locate internal errors of objects called in JSP. Because this will have to frequently compile, publish, start and stop the server, which brings great inconvenience to debugging.
The legendary WYSIWYG JSP debugging tool does not exist as far as the author currently knows. If readers have any information about this, please let me know. But JSP debugging tools do exist, and they are free! It's lomboz. Download address: http://www.objectlearn.com/products/download.jsp
This article starts from a simple example to describe how to use lomboz to debug JSP. The development environment of this example is as follows:
-JDK version: 1.4.1;
-JAVA IDE: Eclipse 2.1;
-lomboz: plug-in for Eclipse2.1;
-JSP/Servlet container: Tomcat 4.1.18.
Introduction to lomboz
Strictly speaking, lomboz is a J2EE plug-in for Eclipse. It integrates many Java application servers, J2EE components and Web application development into Eclipse, which can help Java developers use Eclipse to build, test, and deploy J2EE applications. For the focus of this article, JSP debugging, it also has related support:
-Supports JSP writing, including: syntax coloring and syntax checking;
-Supports JSP debugging.
For JSP debugging, the principle of lomboz is to debug the java code generated by the JSP page to be debugged. The entire debugging process is the same as that of a java application, so that both the debugging of the JSP itself and the debugging of the object called by the JSP are seamlessly linked together.
Before starting the examples in this article, let's take a look at the installation of lomboz and the configuration for normal use of its JSP debugging function.
Installation and configuration
The installation process of lomboz is very simple. Most Eclipse plug-in installation processes are very simple. The main steps are as follows:
1. Download from the lomboz download address ( http://www.objectlearn.com/products/download.jsp ) to support Eclipse2. 1 (or other versions of Eclipse).
2. If Eclipse is already running, please close it first.
3. Extract the downloaded file to the plugins directory in the Eclipse installation directory.
4. Restart Eclipse and open "About Eclipse Platform Plug-ins" (Help -> About Eclipse Platform ->Plug-in Details). If lomboz is installed successfully, information about it will appear. As shown below:
Note: If you want to uninstall lomboz, you only need to delete the directory corresponding to lomboz in the plugins directory.
5. Set up Lomboz J2EE View, open Window -> Customize Perspective, as shown in the figure:
1. Configure JDK, select "Windows -> Preferences", select Lomboz, and configure JDK. As shown in the legend:
2. Define the server. If your server is already included in Lomboz's default server list, this step can be skipped. The Lomboz default server list can be obtained by expanding Lomboz in the interface of the previous step, and then selecting "Server Definitions". The following takes adding Tomcat4.1.18 as an example to explain how to add a server. The relevant steps are as follows:
a) Enter the servers subdirectory of the Lomboz installation directory, copy a .server file (such as tomcat410.server), and rename it to tomcat418.server.
b) Open this file and change the following locations of this file:
- <serverDefinition....../>, change the value of name to the name you want (such as Apache Tomcat v4.1.8), this name is used to display in The server list part of the "Server Definitions" interface;
- <property id="serverRootDirectory" ........./>, where the default value is changed to the directory where Tomcat is located on your machine, such as D:/Program Files/Java/tomcat- 4.1.18;
- <property id="classPathVariableName" .../>, where the default value is changed to any name (such as TOMCAT418). This name is used to represent the default ClassPath variable name of the Lomboz project;
- <property id ="classPath" .../>, where the default value is changed to the directory where Tomcat is located on your machine, such as D:/Program Files/Java/tomcat-4.1.18;
c) Add jasper-runtime.jar. By default, the TomcatX.server file contains only 2 jar files: servlet.jar and bootstrap.jar, but jasper-runtime.jar is missing. This jar file plays a very important role in the JSP debugging process, so please add it. There are two ways to add a jar to a server:
Method 1:
Add a line in the <serverClassPath> section of the .server file you just edited: <jar type="variable">${classPathVariableName}/common/lib/jasper-runtime. jar</jar>;
Method 2: Use the "Server Definitions" interface, which will not be described again here.
The configured server will be displayed in the "Server Definitions" form, as shown below:
Note: Tomcat4.x servers need to be patched to make Lomboz work properly. The download address of the patch is:
At this point, the installation and configuration of Lomboz have been completed, so let us create a " Hello World!" example to experience the process of Lomboz debugging JSP.
Debugging JSPs
Create a project To use Lomboz to debug JSP, you must first create a Lomboz project. The steps are as follows:
1. Open File->New->Lomboz J2EE Project and create a project named debugJsp;
2. Select "Next" to enter the Lomboz project property selection page and add the Web module and the server used. Since our main task is to debug JSP, the EJB module can be empty. As shown below:
3. Select Finish, and the Lomboz project is created. At this time, the file structure generated by Lomboz is as follows:
Next, we can create the JSP files we need. In this example, I directly use the default index.jsp and change its content to:
<!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->
<html>
<head>
<title>Welcome</title>
</head>
<body>
<center><%= "Hello World!"%></center>
</body>
</html>
So far, our preparations for debugging JSP have been roughly completed, but those who use Tomcat4.x still need extra steps (these steps are mainly caused by tomcat4.x itself):
1. In Tomcat Add the following lines to the server.xml file in the config directory (for specific locations, please refer to the documentation related to server.xml):
<Context path="/TestJsp" docBase="D:DebugJspdebugJsp" workDir="D: DebugJspj2srcorgapachejsp">
Among them, path represents the URL name of the web application; docBase represents the physical location of the web application; workDir represents the location where the java file corresponding to jsp is generated. You must create orgapachejsp like this Directory structure and name.
2. Create the corresponding subdirectory structure in the corresponding j2src directory according to orgapachejsp, and set j2src as the source directory of the project.
Syntax Check Now, let's enjoy the process of debugging JSP. Before debugging, the JSP should have no syntax errors. Lomboz provides the function of JSP syntax checking. The specific operation is:
1. Select the JSP file to be syntax checked and right-click;
2. Select Lomboz j2ee... -> Check Jsp Syntax
After debugging and solving the syntax errors, the next step is Time to address the logic errors. This kind of error can only be solved through debugging. The process of using Lomboz to debug JSP is very similar to the debugging of ordinary JAVA applications, because its principle is to first use the server to generate JAVA files corresponding to JSP, and then debug them.
The steps to debug JSP are as follows:
1. Open Lomboz J2EE View, select the server, right-click the mouse and select "debug server". As shown in the picture:
If the .server file is defined correctly, Tomcat will start. If Tomcat does not start as expected, please troubleshoot from these two aspects: Tomcat environment variables and Lomboz's server file.
2. Open a browser, such as IE, and enter: http://127.0.0.1:8080/debugJsp/index.jsp in the address bar. If there is no syntax error in the JSP file, the JAVA file corresponding to the JSP file will be generated in the j2srcorgapachejsp directory of the project. Refresh the project to see these files. As shown in the picture:
This step is mainly to trigger the server to generate java files for debugging based on jsp. You don't have to worry too much about whether the results are correct. Because once the java file is generated, these errors can be eliminated through debugging.
3. Open the java file, set a breakpoint in it, and then enter the URL again in the address bar of IE. At this time, the debugging function of Eclipse will be activated. At this point, JSP can be debugged in a targeted manner. The situation at this time is as shown in the figure:
As for the debugging of the object called by JSP, after completing the above steps, then in the java corresponding to the object
Just set breakpoints in the file.
Conclusion Although the current debugging of JSP is not as convenient as the debugging of ordinary java files, the emergence of tools such as Lomboz has at least ended the stage of manual debugging of JSP. From now on, Java web application developers do not have to stop the program from time to time to manually add print statements to observe the values of variables due to some errors. On the contrary, they can observe changes in these values while the program is running just like debugging ordinary JAVA applications. This greatly facilitates the development of WEB-based applications and reduces the time previously wasted on debugging.