It's not a secret anymore!
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Question regarding servlet-mapping in web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Question regarding servlet-mapping in web.xml" Watch "Question regarding servlet-mapping in web.xml" New topic

Question regarding servlet-mapping in web.xml

prad argum

Joined: Feb 18, 2003
Posts: 11
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.

<html><head></head><body><form action="TestServlet/" method="POST">Technology: <input type="text" name="searchstring" value="java"><br><br>State :<select name="state" size="5" multiple><option value="NJ">New Jersey</option><option value="NY">New York</option><option value="PA">PA</option><option value="Kan">Kansas</option><option value="Cal">California</option><option value="Tex">Texas</option></select><br><br><input type="submit" value="Search Job" id="Submit1" name="Submit1"></form></body></html>
import javax.servlet.*;import javax.servlet.http.*;import*;public class TestServlet extends HttpServlet{public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{String searchString = req.getParameter("searchstring");String[] stateList = req.getParameterValues("state");PrintWriter pw = res.getWriter();pw.println("<html>");pw.println("<head>");pw.println("</head>");pw.println("<body>");pw.println(searchString + "<br>");for (int i=0;i<stateList.length;i++) { pw.println(stateList[i] + "<br>"); }pw.println("</body>");pw.println("</html>");}}
web.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" ""><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></web-app>

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 :
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" ""><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
chatwithpratap chatwithpratap

Joined: Mar 31, 2003
Posts: 2
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.
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
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?
Lionel Port

Joined: Sep 14, 2002
Posts: 19
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)
S. Ganapathy
Ranch Hand

Joined: Mar 26, 2003
Posts: 194
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.
prad argum

Joined: Feb 18, 2003
Posts: 11
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.
Amer Khan
Ranch Hand

Joined: Apr 05, 2003
Posts: 163
this the default url,There is no physical directory [servlet],its just represents the invoker servlet.if you use mapping for myServlet
[DEFAULT url][invoker-can be disabled --conf/web.xml]comment out the invoker servlet.
2) host/webApplicationPrefix/myServlet
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>
I agree. Here's the link:
subject: Question regarding servlet-mapping in web.xml
It's not a secret anymore!