1. My web app's name is 'king'. I believe that in the <url-pattern> tag I need not give a /king/test.do path, i-e there's no need to precede the web app name here. But, why is it that way?
2. In a servlet, If i obtain a RequestDisatcher through a request, I have two oprions: (a) request.getRequestDispatcher("test.do"); OR (b) request.getRequestDispatcher("/test.do"); Both options work, but, which one is better?
3. If I want to connect to TestServlet from a HTML page, I need to give href="/king/test.do", Why is the app name (king) required?
1. for identifing the context you should always mention the app's name
2. The pathname specified cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. and requestdispature always points to current context.
3. HTML means completly at client side so that HTML page don't know about yor app's name or context name so you need to mention complete url in "href"
1)For every web application there is war file ,so in application.xml or someother file give the name and will map with contextroot name.It will be as below <module id="WebModule_1155931475921"> <web> <web-uri>KingWar.war</web-uri> <context-root>king</context-root> </web> </module>
So when you go for the request king/test.do first it will find out the context root .. here king.. then go to the corresponding war file.. As you know for every war file there will be web.xml from there it identify the Servlet for which the request will be delegate,,,,
This is how the whole process will be going.. dont confuse with application.xml simply takes that it is war file name... king..
when you go for Servletcontext the path must start with "/" .. as it has to start from context root..
when you go for ServletRequest the path may or may not start with"/" ..
which is better means? i dont know exactly..
3)For this Akbar given nice answer,,
If you still having doubts.
Pls feel free to post
Joined: Jan 26, 2006
Thanks Akbar and Madhav. But, I still have a query.
Akbar's post- "for identifing the context you should always mention the app's name"
But, in url-pattern tag I just need to mention the logical resource's name..right?
I also tried this:
This gave me 404 Not Found.
Is my understanding correct?
Joined: Feb 09, 2007
you are right?
you need to mention like this.. <url-pattern>/test.do</url-pattern> king is yor context name ...right in browser first you need to type like "http://hostname:1100/king" it identifies your web.xml file in other sence browser points to your application context... from ther you need to mention "test.do" in action tag of HTML page. that will directly invokes coresponding servlet