File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to call a servlet from a html Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to call a servlet from a html" Watch "How to call a servlet from a html" New topic
Author

How to call a servlet from a html

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi all

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
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.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Virendrasinh Gohil wrote: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.


Hi

The web.xml is present in the folder:
E:\SONNY\WORKSPACE\ServletTest\WebContent\WEB-INF

and has the mapping for <FormServlet> It's source is as follows:
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
Great. Now can you tell me what is the deployment structure.

Here you are requesting a servlet using
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?)
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Virendrasinh Gohil wrote:Great. Now can you tell me what is the deployment structure.

Here you are requesting a servlet using
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
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.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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
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.

uri is : /ServletTest/FormServlet
If your application would have executed properly, you would have got http://localhost:10139/ServletTest/FormServlet

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.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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.

Regards
Mansukhdeep
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

When you define your servlet class you have to give it the fully-qualified class name, including the package.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Virendrasinh Gohil wrote:
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.

uri is : /ServletTest/FormServlet
If your application would have executed properly, you would have got http://localhost:10139/ServletTest/FormServlet

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.

Regards
Mansukhdeep
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

David Newton wrote:When you define your servlet class you have to give it the fully-qualified class name, including the package.



Ok. So you mean the <servlet-class> tag in the web.xml for the FormServlet changes from:

to


Is that correct?

Regards
Mansukhdeep
Soumitra Biswash
Greenhorn

Joined: Aug 12, 2010
Posts: 3
Hi..

"Working out to integrate OpenLDAP in Liferay to incorporate single sign-on feature" looking for input for the same.

Thanks,
Soumitra
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Soumitra: Please see the JavaRanch naming policy and change your display name to conform with this policy. Thanks!

Also, please choose an appropriate forum for your message.
Soumitra Biswash
Greenhorn

Joined: Aug 12, 2010
Posts: 3
@David: Thanks for your suggestion david . Will do as per your guidline but still i am proud to be associated with Sigma Infosolutions.

Have a great day ...

Thanks,

Soumitra
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Sorry Soumitra, rules are rules. Be aware accounts with names that do not comply with our naming policy will be deleted.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Sigma Infosolutions wrote:but still i am proud to be associated with Sigma Infosolutions.

Proud or not, please check your private messages.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Soumitra Biswash
Greenhorn

Joined: Aug 12, 2010
Posts: 3
Hello friends ,

I have made the necessary changes ...

@Bear Bibeault: Your private message was really scary ...

Thanks everyone for your help. Be in touch .. take care .. cya

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to call a servlet from a html