>>> Jiger Patel <[EMAIL PROTECTED]> 06-Feb-01 5:18:47 AM >>>

Before I start to answer this you should be aware that this is the
wierdest part of the API (it's not really the APIs fault that it's
wierd - it's the fault of webservers and the way we use operating
systems).


> Now my Client if asking for a URL like
>  java myclient "http://m1:8080/eShoppe"
>
>then it gives back 302 Found error.
>
>Then if I add a slash to my url Like this
>  java myclient "http://m1:8080/eShoppe/"
>
>then it gives me index.jsp as wanted. This is when
>my Servlet Engine is JWSDK.

Don't use JWSDK: it's magnificently out of date.

>Now I start tomcat & go thru the same procedure but
>neither with nor without slash returns the index.jsp.

What does Tomcat return? 302?


>But If I use my browser to ask for that same URL then
>it returns the index.jsp I also went thru the HTTP specs
>but didnt find anything missing.

This problem is related to mappings and to webservers. Note that a
webserver is different from a servlet engine, a webserver serves
static file like images and html files (consider a JSP a static page
for now).

There are, oooh, about a zillion different combinations and different
ways of matching a path to the correct servlet. The servlet API
specifies that it must be done by specifying a servlet against a path
mapping of some kind.


First, understand that a path ending in a slash is what we call a
directory mapping.

A path that doesn't end in a slash and where the last part of the
path has a "." in it is called a file-mapping.

So these are some directory mappings:

   /dir1/
   /dir1/dir2/

and these are some file mappings:

   /dir1/file.txt
   /dir1/dir2/file.jsp

So what is this?

  /dir1/file.jsp/

A file mapping? Wrong! It's a directory mapping. And this:

   /dir1.jsp/dir1.jsp

is a file mapping.


But: what if a path that hasn't got an end "/" or a last part "."?
It's just neither one nor the other. It can't really be classified as
a directory mapping because there might be a file called dir1 and if
it was considered a directory it wouldn't get served. But it can't be
considered as a file mapping either because if it was a directory then
it wouldn't get served. So the webserver has to guess what you mean
based on the contents of the directory.

The servlet engine (Tomcat not JWSDK - that's very old and probably
broken) can map servlets to basically any path:

  /dir1/
  /dir1/file.jsp
  /dir1

Within the servlet engine the mapping:

  /dir1

is treated just the same as a directory mapping.

But this is not how the webserver sees it. Remember that the
webserver is the default servlet. What is the default servlet? The
default servlet is the servlet that recieves all requests that haven't
been matched to another servlet.

So if you ask the server for /dir1 and you don't have a mapping for
it within the servlet engine then the default servlet (also known as
the webserver servlet or the file servlet) gets it.

The webserver servlet behaves like a webserver and webservers have
rules about paths that aren't either file paths or directory paths.

The rule goes like this:

- if there is a file with this name then return the file
- if there is a directory with this name then return a redirect to
the proper directory name

So if on server nics.tfltd.net:9000 you have a directory:

  /dir1
    /index.jsp
    /pamela.jpg
    /brad.gif

and you request this:

  nic.tfltd.net:9000/dir1

when the default servlet recieves your request it will:

- search for a file dir1
- not find one
- search for a directory dir1/
- find one
- send you a 302 response with the new location: /dir1/

If you were using a browser the browser would automatically follow
the 302 response to the new location which is why you can type a
non-directory path into the browser and get the response you want.

If you want a Java client to behave the same way you have to
implement the correct response to the 302.

What is the coirrect action to take on recieving 302? You just issue
another request to the server for the directory specified in the
"Location:" header of the 302 response.

So your code looks something like:

  String path=getArgument();
  int code=sendRequestToServer(path);
  while(code==302)
  {
     path=getHeader("Location");
     code=sendRequestToServer(path);
   }


If you choose to use an HttpURLConnection this is all done for you by
setting followRedirects on the connection before you open it.

I'd really advise you to use the HttpURLConnection rather than
writing direct to the Socket because it could solve other problems for
you as well.


Nic

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to