• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

newbie prob (Request Identification)

Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I was asked the following question in an interview.

"Supposing a servlet recieves two requests from different clients at the same time. Client A needs to search the new movies in town. Client B needs to search a magazine section. The servlet provides both these services. It generates the required responses. Now, how does it know whom to send which response?"

I responded with all that i knew about session management and the connector(COYOTE), container(CATALINA) architecture of APACHE TOMCAT. The interviewer said I was close, but not correct.

I've tried googling, and re-read the first few chapters of J.Hunter's book. Did not find any great explanations.

Is it the job of the servlet to identify the clients of individual requests to send the response back?

I've done some servlet coding(tech. demonstration purposes & school requirements), but not in a production environment.(Just Outta School!)

Do people in the industry code their servlets to respond to their clients based on their IP addresses? If so, why aren't these procedures mentioned in the textbooks?

Any explanation will be greatly appreciated.
[ August 21, 2004: Message edited by: chowdary Thammineedi ]
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The doPost or doGet method always recieves request and response together. And response will always be sent to where request originated from.
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, let's up the ante a little. You have two browsers on the same machine that each submit different requests to the same servlet. The servlet receives the request details in the HTTP message header. For both requests a sniffer might show the output as follows:

Request information
Request Method: GET
Request URI: /demo/snoop.jsp
Request Protocol: HTTP/1.1
Servlet Path: /demo/snoop.jsp
Path Info: null
Path Translated: null
Query String: null
Content Length: -1
Content Type: null
Server Name: www.my-server.co.uk
Server Port: 80
Remote User: null
Remote Address:
Remote Host:
Authorization Scheme: null

Request headers
host: www.my-server.co.uk:20145
accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, application/x-shockwave-flash, */*
accept-encoding: gzip
accept-language: en-gb
referer: http://web-sniffer.net/
user-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461) Web-Sniffer/1.0.14
x-forwarded-host: www.my-server.co.uk
x-forwarded-server: www.my-server.co.uk
connection: close

The response from one servlet contains the following (for example):

HTTP Status Code: HTTP/1.0 200 OK
Date: Fri, 20 Aug 2004 15:07:43 GMT
Server: Apache Tomcat/4.0.1 (HTTP/1.1 Connector)
Content-Type: text/html;charset=ISO-8859-1
Pragma: no-cache
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Expires: Wed, 31 Dec 1969 23:59:59 GMT
Set-Cookie: JSESSIONID=A8869016768081C245F3907770E01299;Path=/
X-Cache: MISS from www.my-server.co.uk
Connection: close CRLF

The second servlet response differs only in the JSESSIONID cookie.

So, when the two responses come back, how does browser A know that it's response is response A and not browser B's response B? Well, I believe that the answer is that this is not part of the HTTP protocol; it's handled in the layers below by TCP. Each browser will be listening on a different TCP port for the response to their request and that's how they know the difference. At the server-side the response is directed to the appropriate TCP port, but it's not part of the HTTP protocol.

So, what's the point of the JSESSIONID cookie then? The next time a request is submitted to the same servlet the cookie is sent with the request headers and the JSESSIONID identifies it as being part of the same session as previous requests.

So, in conclusion, the session ID has nothing to do with how responses are routed from the server to the browser, although it is passed back with the response. It comes into its own in tying together a sequence of requests.

I think everything I've said is pretty much accurate, but if I'm not 100% on the mark then please correct me.

Hope this is useful and interesting.

chowdary Thammineedi
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jules.

I was thinking along the same lines too.

The servlet container recieves the (request,response) bundle from the connector, processes the request, generates the response, bundles them together again and sends it back to the connector. The connector then has to examine it, identify the request, lookup the corresponding client and send the response back.

Am i Thinking Right? May be I need to get my TCP funda in order!

Thanks again for the reply.
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic