Forums Register Login
Html pages doesn't call servlet
I have folders in apache like this

C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.26\webapps\ROOT

in ROOT folder I've created my folder named myContext

C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.26\webapps\ROOT\myContext

in that myContext folder I've created WEB-INF folder in that classes and lib folders like this
C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.26\webapps\ROOT\myContext\WEB-INF\classes
C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.26\webapps\ROOT\myContext\WEB-INF\lib

Now in myContext I've created main.html page which calls to servlet TemplateServlet.class file which is stored in classes folder

C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.26\webapps\ROOT\myContext\main.html

main.html file contains

<title>Main Page</title>
<form action="test" method="post">
<center><h1>Main Page</h1></center>
<input type="submit" value="Submit" >

C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.26\webapps\ROOT\myContext\WEB-INF\web.xml file contains

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




C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.26\webapps\ROOT\myContext\WEB-INF\classes\TemplateServlet.class contains compiled code of TemplateServlet.java which contains

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TemplateServlet extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
PrintWriter out = response.getWriter();


When I type in browser http://localhost:8084/myContext/main.html
It shows main.html but when i click on submit button it gives following error

HTTP Status 404 - /myContext/test

type Status report

message /myContext/test

description The requested resource (/myContext/test) is not available.

Apache Tomcat/6.0.26

Somebody please help me in this i heard i have to set something getContext path or sth dunno what it is so please guide me
Hi Ganesh,

Please use code tags for any code you post. Its easy to read.
Try adding fully qualified class name for TemplateServlet.

here in action, servlet name is test.

but I see your servlet class name is -'TemplateServlet'

A common beginner error. ALL classes used in servlets should be in a package and the class file in the corresponding directory.

The reason being that without an explicit package, the JVM looks in the "current" directory - something you have no control over.

@ Divya I dunno but what i have learned according to that when click on Submit button it will submit test then it will go to web.xml file and in that in servlet mapping means here

it will search url-pattern test and now here we have url pattern test and now will see servlet-name i.e template and this name will be mapped to servlet-name in <servlet> here in below code

and when it matched to servlet -name i.e template it will go for servlet class file name i.e TemplateServlet and will search this TemplateServlet.class file in WEB-INF /classes folder in classes folder I have TemplateServlet.class i.e compiled java file now it will run that thats what i have been told

Dunno whether I'm correct or not but please correct me if I'm wrong
@ William Will you please elaborate it. I haven't understood which files to be put in which folder.

See I have webapps/ROOT folders.

In this ROOt folder I kept my html page

Ex. ROOT/Prog2.html and
In ROOT folder I have create WEB-INF folder in this folder I have web.sml file which do mapping n all.

Ex. ROOT/WEB-INF/web.xml

In same WEB-INF folder I have created a folder named classes which contains .class file

Ex. ROOT/WEB-INF/classes/TemplateServlet.class

Please correct me if i'm wrong
@ T Mishra ya sure friend. I didn't know that . What do you mean Try adding fully qualified class name for TemplateServlet. sorry didn't get that please explain.
Based on the config setup, it looks ok. The servlet class in the web.xml file should be the fully qualified name (with package if any).

The culprit is in your servlet code. You only have a doGet() method. And your html form is using POST. Changing the doGet to doPost should get your output when the form is submitted.

If you don't use the form at all and just type in the servlet url, you should get your output. Try it.
k. Tsang ya i will try it now thank you
All classes used in servlets MUST be in a package or you get confusing errors.

The package name MUST be used in the directory structure for the .class file, so if your servlet package is "my.spot"

Instead of locating it in

you MUST use a path under classes that shows the package name - that is how the servlet container finds and loads the class.


(confusion may result from looking at older references which used the dreaded Invoker Servlet supposedly to make things easier but actually confusing generations of programmers)

@ William Thanks a lot for help. I will try with this
OMG !! hey friends It wasn't a problem of my code. I've successfully run this program with same code as typed at the beginning and also got correct output.

You know the problem was in version of Tomcat. Actually I had run above program using jakarta-tomcat-5.5.9. I installed that and copied all files as I mentioned above and deleted all content of web.xml and copied this in that

<web-app xmlns="http://java.sun.com/j2ee/xml/ns" version="2.4">




that's it it ran I will figure out soon It was really problem of Tomcat version or content of my web.xml file
Finally got the answer it wan't problem of tomcat version. It ran because of the <web-app xmlns="http://java.sun.com/j2ee/xml/ns" version="2.4">
this code. Thank you all for your valuable support.

Ganesh Pat wrote:Finally got the answer it wan't problem of tomcat version. It ran because of the <web-app xmlns="http://java.sun.com/j2ee/xml/ns" version="2.4">
this code. Thank you all for your valuable support.

If your web.xml is using (servlets) 2.4, then your web app is developing against J2EE 1.4.

If you use a newer version of Tomcat, ideally it should work because it's backward compatible.

This thread has been viewed 1562 times.

All times above are in ranch (not your local) time.
The current ranch time is
Nov 14, 2018 08:11:12.