Servlet -- Intricacies to be known
First : it is necessary that you have to start with "/" in <url-pattern> tag like below, if you omit the "/", the tomcat server or any other we server will ail to start saying invalid url patter matcher.
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
Second : the url pattern get match strictly with what you had specify in <url-pattern> tag,
So if have written like :
<url-pattern>/Hello</url-pattern> then, the browser will run the associated servlet to this url when you type specifically:
http://localhost:8080/Hello only.
http://localhost:8080/Hello/ will not work and say requested page not found.
Third : if you have specified
<welcome-file>default.html | .jsp | .htm</welcome-file> but you have not matched the root path
"/" to any servlet, i.e, <url-pattern>/</url-pattern> has not defined, then this <Welcome-file> page will get load when you request root , http://localhost:8080 or http://localhost:8080/ , for root path "/" , you may omit the "/" from web address as we did above and will work fine, but for the others as mentioned in Second Point above its strict.
Fourth : For the root path if you had specified the <url-pattern>/</url-pattern> then that corresponding servlet attached to root path will be executed and in this case even if you have specify
<welcome-file>default.html | .jsp | .htm</welcome-file> tag, this welcome file will not be served because the servlet will get preference over welcome file this time.
Fifth : say the <url-pattern>/</url-pattern> is there, so obviously as mentioned in point Four above the servlet will be executed irrespective of <welcome-file/> tag is present or not.
Now suppose there are two servlet were declared one for root "/" and one for "/Hello" :
<url-pattern>/</url-pattern>
<url-pattern>/Hello</url-pattern>
then as described in point Second above, the corresponding path will be strictly check against the
<url-pattern> tag and corresponding servlet will get executed, so :
on typing
http://localhost:8080/Hello the servlet attached to /Hello get executed,
but when you type:
http://localhost:8080/Hello/ see the extra / at end of this path , as we said server will now check for absolute path /Hello/ to find a servlet, but since we have the one defined for /Hello but not for /Hello/, so if we wouldn't had defined <url-pattern>/</url-pattern>, then server would had sent in response "requested url not found", but since we have servlet defined for "/" that extra "/" at the end of "/Hello/" will now run the servlet correspond to "/" path, so even if one has typed :
http://localhost:8080/Hello/World even this will also run the servlet assigned for root path "/" instead of giving "requested url not found error".
However the same is not true for <welcome-file> tag present and <url-pattern>/</url-pattern> absent, in this case if user has typed http://localhost:8080/Hello/ or http://localhost:8080/Hello/World, the welcome page tag file will not be given in response ,rather "the requested url not found error" will be returned.
Sixth : There is a concept of WebModule, which get deployed to a web server, and every web module has context path from which its servlet will come into picture:
Say a web module named Webb is having context as "/"
so everytime a person hit http://localhost:8080/ or http://localhost:8080 this module(Webb) will be in effect and the servlet matched to <url-pattern>/</url-pattern> or if this tag is absent the welcome page under that module will be served.
Now say there is one more module named Modle has also deployed and its context path you given is '/Mod" (Note no two module can have the same context path defined, the server will throw exception)
Now here is a scenario, say the Webb module has a servlet which is mapped at <url-pattern>/Mod</url-pattern>, here to notice the path given has the context path of one of the module "Modle", so the catch here is if the user at one of the servlet at Webb module post or access to /Mod, the server will first check if the path is a context of one of the module which is true in this case as the form posted to /Mod belongs to one of the context path of module Modle. thus the server will post the request to context path "/Mod" and remember each context path again has their own "/" root mappings, so server will check is there any root path "/" has defined for Modle (because /Mod is the context path and server always look for the path following the context path in this case an implicit "/"), thus if a servlet is defined for <url-pattern>/</url-pattern> in Modle that will handle the form posted by Webb Module.
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
Second : the url pattern get match strictly with what you had specify in <url-pattern> tag,
So if have written like :
<url-pattern>/Hello</url-pattern> then, the browser will run the associated servlet to this url when you type specifically:
http://localhost:8080/Hello only.
http://localhost:8080/Hello/ will not work and say requested page not found.
Third : if you have specified
<welcome-file>default.html | .jsp | .htm</welcome-file> but you have not matched the root path
"/" to any servlet, i.e, <url-pattern>/</url-pattern> has not defined, then this <Welcome-file> page will get load when you request root , http://localhost:8080 or http://localhost:8080/ , for root path "/" , you may omit the "/" from web address as we did above and will work fine, but for the others as mentioned in Second Point above its strict.
Fourth : For the root path if you had specified the <url-pattern>/</url-pattern> then that corresponding servlet attached to root path will be executed and in this case even if you have specify
<welcome-file>default.html | .jsp | .htm</welcome-file> tag, this welcome file will not be served because the servlet will get preference over welcome file this time.
Fifth : say the <url-pattern>/</url-pattern> is there, so obviously as mentioned in point Four above the servlet will be executed irrespective of <welcome-file/> tag is present or not.
Now suppose there are two servlet were declared one for root "/" and one for "/Hello" :
<url-pattern>/</url-pattern>
<url-pattern>/Hello</url-pattern>
then as described in point Second above, the corresponding path will be strictly check against the
<url-pattern> tag and corresponding servlet will get executed, so :
on typing
http://localhost:8080/Hello the servlet attached to /Hello get executed,
but when you type:
http://localhost:8080/Hello/ see the extra / at end of this path , as we said server will now check for absolute path /Hello/ to find a servlet, but since we have the one defined for /Hello but not for /Hello/, so if we wouldn't had defined <url-pattern>/</url-pattern>, then server would had sent in response "requested url not found", but since we have servlet defined for "/" that extra "/" at the end of "/Hello/" will now run the servlet correspond to "/" path, so even if one has typed :
http://localhost:8080/Hello/World even this will also run the servlet assigned for root path "/" instead of giving "requested url not found error".
However the same is not true for <welcome-file> tag present and <url-pattern>/</url-pattern> absent, in this case if user has typed http://localhost:8080/Hello/ or http://localhost:8080/Hello/World, the welcome page tag file will not be given in response ,rather "the requested url not found error" will be returned.
Sixth : There is a concept of WebModule, which get deployed to a web server, and every web module has context path from which its servlet will come into picture:
Say a web module named Webb is having context as "/"
so everytime a person hit http://localhost:8080/ or http://localhost:8080 this module(Webb) will be in effect and the servlet matched to <url-pattern>/</url-pattern> or if this tag is absent the welcome page under that module will be served.
Now say there is one more module named Modle has also deployed and its context path you given is '/Mod" (Note no two module can have the same context path defined, the server will throw exception)
Now here is a scenario, say the Webb module has a servlet which is mapped at <url-pattern>/Mod</url-pattern>, here to notice the path given has the context path of one of the module "Modle", so the catch here is if the user at one of the servlet at Webb module post or access to /Mod, the server will first check if the path is a context of one of the module which is true in this case as the form posted to /Mod belongs to one of the context path of module Modle. thus the server will post the request to context path "/Mod" and remember each context path again has their own "/" root mappings, so server will check is there any root path "/" has defined for Modle (because /Mod is the context path and server always look for the path following the context path in this case an implicit "/"), thus if a servlet is defined for <url-pattern>/</url-pattern> in Modle that will handle the form posted by Webb Module.
Comments
Post a Comment