Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to call a servlet from a html

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Virendrasinh Gohil
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you define your servlet class you have to give it the fully-qualified class name, including the package.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Soumitra, rules are rules. Be aware accounts with names that do not comply with our naming policy will be deleted.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65113
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sigma Infosolutions wrote:but still i am proud to be associated with Sigma Infosolutions.

Proud or not, please check your private messages.
 
Soumitra Biswash
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic