Hi all, I am sorry to post this topic again. I am doing this since i dint get any reply previously. My question is regarding the <servlet-mapping> in web.xml file. I would like to know if it is a must to have servlet-mapping for every servlet in the web.xml file. I have written a TestServlet and did not give its <servlet-mapping> in the web.xml file. I was not able to give the correct path in webform for POST action. If I donot give <servlet-mapping>, what would be exact path of this servlet, if i need to access this from some webform for POST or GET action. EXAMPLE: form.html:
In the above web.xml file, since i dint give any servlet mapping, i have tried to access the TestServlet in POST action in form.html by giving path: "TestServlet". But this did not work. But once i have changed the web.xml file to : code: -------------------------------------------------------------------------------- <?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app> <servlet> <servlet-name> HelloWorldServlet </servlet-name> <servlet-class> HelloWorldServlet </servlet-class> </servlet> <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class>TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>/HelloWorldServlet/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet/*</url-pattern> </servlet-mapping></web-app> --------------------------------------------------------------------------------
Now I tried to access the servlet in the webform by giving path "TestServlet" for POST action. This time it worked. If <servlet-mapping> for every servlet is not a must, then please help me giving correct path to access TestServlet in the form.html file. thanking you pradgum
you dont have to give the entry for every servlet. making entry is jsut creating an "allias" for that servlet. i m not sure but : if u dont give mapping then u have to give path relative to ur web-app.... i.e. u have to give web-app/dir1/package/servlet. i guess u cannot access the servlets in web-inf dirs.
I ran across similar problem like Prad's. I tried to execute a servlet without a servlet-mapping. It doesn't work in weblogic 6.1 server. I also read the specification. It doesn't mandate to have a <servlet-mapping> for every <servlet> specified in web.xml. I guess in Tomcat there is an invoker servlet which is responsible to execute the servlets with no <servlet-mapping> element. Anyway, here is my question: for scwcd certification, do we need to worry about this issue? Are there any questions related to this problem?
I think it is generally a good idea to use the <servlet-mapping>. Without it, I don't think the URL location will be handled in a consistent manner across the application servers. Under tomcat the url will be. http://localhost:8080/<web app name>/servlet/<servlet class> where <web app name> is the name of your war file and <servlet class> is the name of the servlet class (including package)
What is this? <form action="TestServlet/" method="POST"> action="TestServlet/", does it carry any meaning??? why cant u try action="servlet/TestServlet" in tomcat??? In action="TestServlet/", which servlet are you refering in TestServlet directory??? Hope by now you got my point. GVRao
Joined: Feb 18, 2003
Hi GvRao, I am sorry that i havent copied the correct web.xml file in the first case. If you look out the modified web.xml file below, i have mapped TestServlet to /TestServlet/*. So the Post action works for that condition. But as you said if i try servlet/TestServlet in form action with out mentioning servlet-mapping of TestServlet to /servlet/*, i dint get the form working. So, my question was is it required to mention servlet-mapping for every servlet. You can give a try once. I think it would be better to mention the servlet mapping for the individual servlets. thanks pradgum
host/webApplicationPrefix/servlet/someServlet this the default url,There is no physical directory [servlet],its just represents the invoker servlet.if you use mapping for myServlet <servlet><servlet-name>hello</servlet-name><servlet-class>myServlet</servlet-class></servlet><servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/*.asp</url-pattern> </servlet-mapping> 1)host/webApplicationPrefix/servlet/myServlet [DEFAULT url][invoker-can be disabled --conf/web.xml]comment out the invoker servlet. 2) host/webApplicationPrefix/myServlet CUSTOM url 3)host/webApplicationPrefix/index.asp or any .asp this url will execute myServlet
<i>Dare to dream - everything that exists today,was once a figment of someone's imagination, nobody says tomorrow can't be a figment of your today.</i>