File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes newbie prob (Request Identification) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "newbie prob (Request Identification)" Watch "newbie prob (Request Identification)" New topic
Author

newbie prob (Request Identification)

chowdary Thammineedi
Ranch Hand

Joined: Aug 16, 2004
Posts: 126
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 ]
Susanta Chatterjee
Ranch Hand

Joined: Aug 12, 2002
Posts: 102
Hi,
The doPost or doGet method always recieves request and response together. And response will always be sent to where request originated from.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
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: 212.74.117.6
Remote Host: 212.74.117.6
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-for: 80.109.215.80
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.

Jules
chowdary Thammineedi
Ranch Hand

Joined: Aug 16, 2004
Posts: 126
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.
 
 
subject: newbie prob (Request Identification)
 
Similar Threads
Best way to implement logging in a servlet based web app
servlet to another client
response without request
Need HTTP API
instance of a servlet