I am writing my first servlet to retrieve form data passed from html.
The html is FirstHtml.html and it is located in a directory:
E:\SONNY\WORKSPACE\ServletTest\WebContent
It is coded as follows:
The name of the servlet I am trying to call is FormServlet.It is located in the following directory:
E:\SONNY\WORKSPACE\ServletTest\build\classes
The html is loading well. But when I press submit after entering the name and age, it says HTTP Status 404 /FormServlet not found. Please educate me as to how to call a servlet from a html file and what are the various ways to do the same.
Regards
Mansukhdeep
~ Mansukh
Virendrasinh Gohil
Ranch Hand
Joined: Jun 09, 2004
Posts: 46
posted
0
Can you please refer "Head First Servlet & JSP" book or any book which explain servlets?
You will learn that there is something named web.xml configuration file which needs to have information of servlets.
which means, while deployment you must have a folder named "servlet" directly in your ROOT of your webapps. (Which server are you using for deployment?)
which means, while deployment you must have a folder named "servlet" directly in your ROOT of your webapps. (Which server are you using for deployment?)
I am using apache tomcat 6.0 integrated with Eclipse Europa IDE. I have a confusion. I just added the following code to my servlet to inquire about the various paths:
and I got the following output:
Context path is /ServletTest
realPath is : E:\SONNY\WORKSPACE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ServletTest\ServletTest\servlet\FormServlet
uri is : /ServletTest/FormServlet
path is : /FormServlet
Please explain these path terminologies and which one is used to call the servlet.
Regards
Mansukhdeep
Amit ChaudhariC
Ranch Hand
Joined: Aug 06, 2009
Posts: 33
posted
0
Context path is /ServletTest
--- This will tell you the ROOT of the webapp.($TOMCATHOME/webapps/)
uri is : /ServletTest/FormServlet
-- This tells you the servlet path from the context
path is : /FormServlet
-- how you can call the servlet while submitting the form.
Instead of
form method = "get" action = "http://localhost:10139/servlet/FormServlet">
try using
form method = "get" action = "/FormServlet">
BTW, it is always good idea to start deploying the web app as a war in tomcat for learning purpose.
Amit ChaudhariC wrote: Context path is /ServletTest
--- This will tell you the ROOT of the webapp.($TOMCATHOME/webapps/)
uri is : /ServletTest/FormServlet
-- This tells you the servlet path from the context
path is : /FormServlet
-- how you can call the servlet while submitting the form.
Instead of
form method = "get" action = "http://localhost:10139/servlet/FormServlet">
try using
form method = "get" action = "/FormServlet">
BTW, it is always good idea to start deploying the web app as a war in tomcat for learning purpose.
Hi Amit
Thanks for the response. Can you suggest an e-book or a link where I can study the very basics of developing Web Application using html forms/servlets/jsp and deployment on tomcat as a war as you said for learning purpose?
Regards
Mansukhdeep
Virendrasinh Gohil
Ranch Hand
Joined: Jun 09, 2004
Posts: 46
posted
0
Mansukhdeep Thind wrote:
I am using apache tomcat 6.0 integrated with Eclipse Europa IDE. I have a confusion. I just added the following code to my servlet to inquire about the various paths:
and I got the following output:
Context path is /ServletTest
realPath is : E:\SONNY\WORKSPACE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ServletTest\ServletTest\servlet\FormServlet
uri is : /ServletTest/FormServlet
path is : /FormServlet
Please explain these path terminologies and which one is used to call the servlet.
Regards
Mansukhdeep
Ok. From this, it seems the simplest solution would be to change your FirstHtml.html and add or , but one more question, how did you get this output if your servlet is not receiving the request? Where did you put this code?
By the way, here goes the explanation for the return types of each method.
Context path is /ServletTest This is the contextpath for the request. This is a relative path for request object. Which means, where is the "request" object for current request sent. This doesn't include the name of the servlet. E.g. For servlets in the default (root) context, it will returns "" as the context for this request is in Root itself.
realPath is : E:\SONNY\WORKSPACE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ServletTest\ServletTest\servlet\FormServlet From my understanding, for requesting real path, you should just provide the relative path of your servlet. Instead of you should have done . This provides you where your servlet class actually resides [physical location]. This method is deprecated.
path is : /FormServlet This returns the path for the servlet (after the context location). Assume that your servlet has package as com.example.FormServlet the output for this would be /com/example/FormServlet. This path is the location of servlet inside of your webapplication (or app context or contextpath)
In sort, typically your contextpath and servlet path combines the relative location of your servlet in Server ROOT.
I think to get better understanding on this, you should understand the terminologies used in web applications.
But the servlet does *not* have that hypothetical package... and it *must* not be in the default package.
So move the servlet into a package (move the source, add the "package" declaration to the source) and update the web.xml configuration file with the new servlet class.
David Newton wrote:But the servlet does *not* have that hypothetical package... and it *must* not be in the default package.
So move the servlet into a package (move the source, add the "package" declaration to the source) and update the web.xml configuration file with the new servlet class.
Thanks David
I created a new package under the src directory by the name com.mansukh.servlets and put the FormServlet.java in to that package. Which tag has to be modified the web.xml? Please assist.
Mansukhdeep Thind wrote:
I am using apache tomcat 6.0 integrated with Eclipse Europa IDE. I have a confusion. I just added the following code to my servlet to inquire about the various paths:
and I got the following output:
Context path is /ServletTest
realPath is : E:\SONNY\WORKSPACE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ServletTest\ServletTest\servlet\FormServlet
uri is : /ServletTest/FormServlet
path is : /FormServlet
Please explain these path terminologies and which one is used to call the servlet.
Regards
Mansukhdeep
Ok. From this, it seems the simplest solution would be to change your FirstHtml.html and add or , but one more question, how did you get this output if your servlet is not receiving the request? Where did you put this code?
By the way, here goes the explanation for the return types of each method.
Context path is /ServletTest This is the contextpath for the request. This is a relative path for request object. Which means, where is the "request" object for current request sent. This doesn't include the name of the servlet. E.g. For servlets in the default (root) context, it will returns "" as the context for this request is in Root itself.
realPath is : E:\SONNY\WORKSPACE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ServletTest\ServletTest\servlet\FormServlet From my understanding, for requesting real path, you should just provide the relative path of your servlet. Instead of you should have done . This provides you where your servlet class actually resides [physical location]. This method is deprecated.
path is : /FormServlet This returns the path for the servlet (after the context location). Assume that your servlet has package as com.example.FormServlet the output for this would be /com/example/FormServlet. This path is the location of servlet inside of your webapplication (or app context or contextpath)
In sort, typically your contextpath and servlet path combines the relative location of your servlet in Server ROOT.
I think to get better understanding on this, you should understand the terminologies used in web applications.
Thanks Viru
I commented the following part of the code:
and the I got added these codes inside the doGet() method of the servlet and ran it as a standalone servlet.
Please only quote relevant portions of messages, otherwise threads become unnecessarily long, and it's not always clear how much we need to pay attention to included text.