JSP Tutorials - Herong's Tutorial Examples - v5.11, by Herong Yang
Archived: Using Perl LWP::Debug Module to Debug
This section provides a tutorial example on how to use Perl LWP::Debug module to help debugging JSP applications at the HTTP request and response level.
Note that LWP::Debug module has been deprecated in Perl. This tutorial example is outdated now.
If you have a problem with your JSP application at the HTTP communication level, one good debugging tool is the Perl LWP module. It can be used as a Web browser to talk to your JSP application, and to log everything at the HTTP communication level.
Here is my sample Perl program, reg_client.pl, designed to work with my previous JSP registration application:
#- reg_client.pl #- Copyright (c) 2006 HerongYang.com. All Rights Reserved. use LWP::Debug qw(+); use LWP::UserAgent; use HTTP::Cookies; ($url) = @ARGV; $url = 'http://localhost' unless $url; $ua = new LWP::UserAgent; $cookie_jar = HTTP::Cookies->new; &getForm(); &submitForm(); exit; sub getForm { $u = $url.'/RegForm.jsp'; my $req = new HTTP::Request GET => $u; my $res = $ua->request($req); $req = $res->request(); $cookie_jar->extract_cookies($res); &dump($req,$res); } sub submitForm { $u = $url .'/RegForm.jsp?name=Mike&pass=None&color=Blue&submit=Submit'; my $req = new HTTP::Request GET => $u; $cookie_jar->add_cookie_header($req); my $res = $ua->request($req); $req = $res->request(); $cookie_jar->extract_cookies($res); &dump($req,$res); } sub dump { local ($req,$res) = @_; print "\nREQUEST-HEADERS\n"; print $req->headers_as_string(); print "\nREQUEST-CONTENT\n"; print $req->content; if ($res->is_success) { print "\nRESPONSE-HEADERS\n"; print $res->headers_as_string(); print "\nRESPONSE-CONTENT\n"; print $res->content; } else { print "\nRESPONSE-ERROR\n"; print $res->error_as_HTML(); } }
A couple of notes to help you to understand this program:
If you run it with "reg_client.pl http://localhost:8080 > client.out" in a command window, you will get the following in the window:
LWP::UserAgent::new: () LWP::UserAgent::request: () LWP::UserAgent::simple_request: GET http://localhost:8080/RegForm.jsp LWP::UserAgent::_need_proxy: Not proxied LWP::Protocol::http::request: () LWP::Protocol::http::request: GET /RegForm.jsp HTTP/1.0 Host: localhost:8080 User-Agent: libwww-perl/5.51 LWP::Protocol::http::request: reading response LWP::Protocol::http::request: HTTP/1.1 200 OK Set-Cookie: JSESSIONID=887896C93DFFF372EB38818BF9F68DB2; Path=/ Content-Type: text/html;charset=UTF-8 Content-Length: 393 Date: Sat, 28 Dec 2002 20:37:04 GMT Server: Apache Coyote/1.0 Connection: close <html><body><b>Registration Form</b>:<br/><form action=RegForm.jsp met hod=get>Login Name:<input type=text size=16 name=name><br/>Password:<i nput type=text size=16 name=pass><br/>Favor Color:<input type=text siz e=16 name=color><br/><input type=submit name=submit value=Submit></br> </form>Your session ID is 887896C93DFFF372EB38818BF9F68DB2<br/>Last us er on the server: Nobody<br/></body></html> LWP::Protocol::http::request: HTTP/1.1 200 OK LWP::Protocol::collect: read 393 bytes LWP::UserAgent::request: Simple response: OK HTTP::Cookies::extract_cookies: Set cookie JSESSIONID => 887896C93DFFF 372EB38818BF9F68DB2 HTTP::Cookies::add_cookie_header: Checking localhost.local for cookies HTTP::Cookies::add_cookie_header: - checking cookie path=/ HTTP::Cookies::add_cookie_header: - checking cookie JSESSIONID=887896 C93DFFF372EB38818BF9F68DB2 HTTP::Cookies::add_cookie_header: it's a match HTTP::Cookies::add_cookie_header: Checking .local for cookies LWP::UserAgent::request: () LWP::UserAgent::simple_request: GET http://localhost:8080/RegForm.jsp? name=Mike&pass=None&color=Blue&submit=Submit LWP::UserAgent::_need_proxy: Not proxied LWP::Protocol::http::request: () LWP::Protocol::http::request: GET /RegForm.jsp?name=Mike&pass=None&col or=Blue&submit=Submit HTTP/1.0 Host: localhost:8080 User-Agent: libwww-perl/5.51 Cookie: JSESSIONID=887896C93DFFF372EB38818BF9F68DB2 Cookie2: $Version=1 LWP::Protocol::http::request: reading response LWP::Protocol::http::request: HTTP/1.1 302 Moved Temporarily Location: http://localhost:8080/RegDone.jsp?color=Blue Content-Type: text/html;charset=UTF-8 Content-Length: 0 Date: Sat, 28 Dec 2002 20:37:04 GMT Server: Apache Coyote/1.0 Connection: close LWP::Protocol::http::request: HTTP/1.1 302 Moved Temporarily LWP::UserAgent::request: Simple response: Found LWP::UserAgent::request: () LWP::UserAgent::simple_request: GET http://localhost:8080/RegDone.jsp? color=Blue LWP::UserAgent::_need_proxy: Not proxied LWP::Protocol::http::request: () LWP::Protocol::http::request: GET /RegDone.jsp?color=Blue HTTP/1.0 Host: localhost:8080 User-Agent: libwww-perl/5.51 Cookie: JSESSIONID=887896C93DFFF372EB38818BF9F68DB2 Cookie2: $Version=1 LWP::Protocol::http::request: reading response LWP::Protocol::http::request: HTTP/1.1 200 OK Content-Type: text/html Content-Length: 221 Date: Sat, 28 Dec 2002 20:37:04 GMT Server: Apache Coyote/1.0 Connection: close LWP::Protocol::http::request: HTTP/1.1 200 OK LWP::Protocol::collect: read 221 bytes LWP::UserAgent::request: Simple response: OK
We have a lot of information here. Let's analyze it quickly.
Table of Contents
JSP (JavaServer Pages) Overview
Tomcat Installation on Windows Systems
Syntax of JSP Pages and JSP Documents
JavaBean Objects and "useBean" Action Elements
Managing HTTP Response Header Lines
Non-ASCII Characters Support in JSP Pages
Overview of JSTL (JSP Standard Tag Libraries)
Multiple Tags Working Together
Using Tomcat on CentOS Systems
Connecting to SQL Server from Servlet
Developing Web Applications with Servlet
Archived: Installing GlassFish JSTL 1.2 on Tomcat
Archived: Downloading and Installing Tomcat 7
Archived: Installing Tomcat 5.5.7
Archived: Installing Tomcat 4.1.18
Archived: Java Class Converted by Tomcat 4.1.18
Archived: Hijacking Servlet Converted from JSP
►Archived: Using Perl LWP::Debug Module to Debug
Archived: Installing JSTL 1.0 Apache Implementation
Archived: Upgrade JDK 1.3 to JDK 1.4 on Tomcat 4.1
Archived: Compilation Errors with JDK 1.4