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:
code:
--------------------------------------------------------------------------------
<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>
--------------------------------------------------------------------------------
TestServlet.java:
code:
--------------------------------------------------------------------------------
import javax.servlet.*;import javax.servlet.http.*;import java.io.*;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:
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></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 :
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