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 How container recognizes a request for servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How container recognizes a request for servlet" Watch "How container recognizes a request for servlet" New topic
Author

How container recognizes a request for servlet

stu ware
Greenhorn

Joined: Feb 04, 2006
Posts: 15
Hi,

Can someone tell me how a container recognizes that the incoming request is for a servlet?

Say http://localhost:8080/hello/javaranch

Here "javaranch" is the servlet class in hello directory. How does the container recognizes that javaranch is a servlet name and then checks the web.xml for the servlet class? There can be other html pages or jsps in hello directory.

So, does it check in the order of html page, jsps and then servlets or is there some thing else which I am missing?

Thanks.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The container looks for servlet and servlet-mappings in your web.xml file.
These map the class files with your URL.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
stu ware
Greenhorn

Joined: Feb 04, 2006
Posts: 15
Hi Ben Souther,

I know that it checks the web.xml file for servlet mapping. What I would like to know is....

Suppose the url is

http://localhost:8080/hello/javaranch.html

The container recognizes that the request is for a html page.

If the request is for http://localhost:8080/hello/javaranch.jsp, the container recognizes that the request is for a jsp page.

But if the url is http://localhost:8080/hello/javaranch how does the container know that the request is for a servlet. There is nothing in the url that suggests the container to look into the web.xml file to find the mapping.

I hope I am clear.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Why not set up a test and see what happens? Seems easy enough.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
stu ware
Greenhorn

Joined: Feb 04, 2006
Posts: 15
Tested it before. Wondering how it was working!!!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
When a J2EE web container receives any request, it first checks it's list of servlet mappings from web.xml. Only if the request is not found in that list does it assume it's a file and display the file. If it is found, it calls the servlet.

In high volume sites, quite often there is a layer of one or more web servers that receive the initial request and then forward the request to the app servers. In this case, there is some type of plugin to the web server that tells it which requests to respond to and which to pass on to the application server. In this case, the data in web.xml is passed on to the web server in some manner (e.g. put in a config file and copied to the web server).


Merrill
Consultant, Sima Solutions
stu ware
Greenhorn

Joined: Feb 04, 2006
Posts: 15
Hey Merrill Higginson,

Awesome!!! You are right.

I wrote a sample test case with one html page and one servlet.

rootdit/first/hi.html is my html page.

Then I created a servlet by name "hi.html" and accessed it as

http://localhost:8080/first/hi.html


I got the output from the servlet instead of the html page.

I got my doubt clarified.

Thanks a lot to all of you.
stu ware
Greenhorn

Joined: Feb 04, 2006
Posts: 15
Hey Merrill Higginson,

Awesome!!! You are right.

I wrote a sample test case with one html page and one servlet.

rootdit/first/hi.html is my html page.

Then I created a servlet by name "hi.html" and accessed it as

http://localhost:8080/first/hi.html


I got the output from the servlet instead of the html page.

I got my doubt clarified.

Thanks a lot to all of you.
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
Thank for both of you ( vasu & Merrill )...it was a nice topic.


Giving up is the easiest thing in the world to do..but holding it together when everything seems like falling apart is true strength!!
with regards, Harish.T
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

"Harish",

You have already been warned on on a previous occasion to change your display name to conform to JavaRanch standards.

This is not optional! Accounts with invalid display names are closed.

Take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

bear
Forum Bartender
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How container recognizes a request for servlet