JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

JSP Sessions and Debugging

Part:   1  2  3  4 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

(Continued from previous part...)

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

#- reg_client.pl
#- Copyright (c) 2002 by Dr. Herong Yang
  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:

  • "use LWP::Debug qw(+);" turns on the debugging at the highest level.
  • A "LWP::UserAgent" object is used to send a HTTP request to the HTTP server.
  • "HTTP:Request" objects are used to compose HTTP requests.
  • "$cookie_jar->extract_cookies($res);" is used to extract cookies from the response. This is very important, because JSP server is sending the session ID as a cookie to the client and expecting the client to send it back in the next request.
  • "$cookie_jar->add_cookie_header($req);" is used to add the cookies received from the previous response to the current request. One of the cookies is the session id, which is important for the JSP server to recognize the current request is a continuation of the previous request.

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.

  • My first request was sent as "GET /RegForm.jsp HTTP/1.0".
  • The first response came back with a cookie as: "JSESSIONID=887896C93DFFF372EB38818BF9F68DB2". Apparently, this is the session ID, but encrypted.
  • My second request was sent as "GET /RegForm.jsp?name=Mike... HTTP/1.0", with two cookies. The first cookie was the JSP server session ID. The second cookie came from nowhere.
  • The second response was interesting. It had code of "302 Moved Temporarily", and a "Location" header line indicating the new URL. Obviously, this response was generated by the "sendRedirect()" function in my JSP page, RegForm.jsp.
  • The LWP::UserAgent object is smart. It recognized the "302 Moved Temporarily" code, and automatically send another request with new URL location.
  • With no surprises, the third response came ok. The JSP did recognize my session ID in my second and third request. But response content was missing in the debugging log for some reason. But the response was captured in the my regular output file, client.out.
  • It is interesting to see that there was no cookie in the second response and third response. My guess is that JSP server saw the session ID in the second request and third request, so there was no need to put the session ID as a cookie in the responses.

Part:   1  2  3  4 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - JSP Sessions and Debugging