Using Perl LWP Package for Debugging

This section provides a tutorial example on how to use the Perl LWP package to debug JSP applications. The LWP package allows to dump HTTP request and response. It also allows you to control how to manage page redirect.

If you have a problem with your JSP application at the HTTP communication level, one good debugging tool is the Perl LWP package. It can be used as a Web browser to talk to your JSP application and to dump complete HTTP request and response messages.

Here is my sample Perl program, reg_client.pl, designed to work with my previous JSP registration application:

#- reg_client.pl
#- Copyright (c) 2012, HerongYang.com, All Rights Reserved.

  use LWP::UserAgent;
  use HTTP::Cookies;
  ($url) = @ARGV;
  $url =  'http://localhost:8080' unless $url;
  $ua = new LWP::UserAgent;
  $ua->add_handler("request_send",  sub { shift->dump; return });
  $ua->add_handler("response_done", sub { shift->dump; return });
  $cookie_jar = HTTP::Cookies->new;
  &getForm();
  &submitForm();
  exit;

sub getForm {
  $u =  $url.'/RegForm.jspx';
  my $req = new HTTP::Request GET => $u;
  my $res = $ua->request($req);
  $req = $res->request();
  $cookie_jar->extract_cookies($res);
  print "\n... FORM REQUESTED ...\n";
}

sub submitForm {
  $u =  $url.'/RegForm.jspx?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);
  print "\n... FORM SUBMITTED ...\n";
}

sub LWP::UserAgent::redirect_ok {
  my ($self, $req, $res) = @_;
  $cookie_jar->add_cookie_header($req);
  1;
}

A couple of notes to help you to understand this program:

If you run it with "reg_client.pl http://localhost:8080" in a command window, you will get the following in the window:

GET http://localhost:8080/RegForm.jspx
User-Agent: libwww-perl/5.836

(no content)

HTTP/1.1 200 OK
Connection: close
Date: Tue, 06 Nov 2012 03:25:40 GMT
Server: Apache-Coyote/1.1
Content-Length: 392
Content-Type: text/html;charset=UTF-8
Client-Date: Tue, 06 Nov 2012 03:25:40 GMT
Client-Peer: 127.0.0.1:8080
Client-Response-Num: 1
Set-Cookie: JSESSIONID=B7C0C3C26958895138C0292B887C8C52; Path=/; 
   HttpOnly

<html><body><b>Registration Form</b>:<br/>
<form action=RegForm.jspx method=get>
Login Name:<input type=text size=16 name=name><br/>
Password:<input type=text size=16 name=pass><br/>
Favor Color:<input type=text size=16 name=color><br/>
<input type=submit name=submit value=Submit></br>
</form>
Your session ID is B7C0C3C26958895138C0292B887C8C52<br/>
Last user on the server: Nobody<br/>
</body></html>

... FORM REQUESTED ...


GET http://localhost:8080/RegForm.jspx?name=Mike&pass=None&color=Blue
   &submit=Submit
User-Agent: libwww-perl/5.836
Cookie: JSESSIONID=B7C0C3C26958895138C0292B887C8C52
Cookie2: $Version="1"

(no content)

HTTP/1.1 302 Found
Connection: close
Date: Tue, 06 Nov 2012 03:25:40 GMT
Location: http://localhost:8080/RegDone.jspx?color=Blue
Server: Apache-Coyote/1.1
Content-Length: 0
Content-Type: text/html;charset=UTF-8
Client-Date: Tue, 06 Nov 2012 03:25:40 GMT
Client-Peer: 127.0.0.1:8080
Client-Response-Num: 1

(no content)

GET http://localhost:8080/RegDone.jspx?color=Blue
User-Agent: libwww-perl/5.836
Cookie: JSESSIONID=B7C0C3C26958895138C0292B887C8C52
Cookie2: $Version="1"

(no content)

HTTP/1.1 200 OK
Connection: close
Date: Tue, 06 Nov 2012 03:25:40 GMT
Server: Apache-Coyote/1.1
Content-Length: 224
Content-Type: text/html;charset=UTF-8
Client-Date: Tue, 06 Nov 2012 03:25:40 GMT
Client-Peer: 127.0.0.1:8080
Client-Response-Num: 1

<html><body><b>Thank you for registering with us</b>:<br/>
Login Name: Mike<br/>Password: None<br/>
Favor Color: Blue<br/>
Your session ID is B7C0C3C26958895138C0292B887C8C52<br/>
Last user on the server: Mike<br/>
</body></html>

... FORM SUBMITTED ...

We have a lot of information here. Let's analyze it quickly.

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

JSP Application Session

 What Is a Session?

 The "session" Implicit Object

 Passing Values between JSP Pages

 Testing Result of RegForm.jspx

Using Perl LWP Package for Debugging

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials

 References

 PDF Printing Version