• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Html pages doesn't call servlet

 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
and
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

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


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"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>template</servlet-name>
<servlet-class>TemplateServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>template</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>



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
{
response.setContentType("text/plain");
PrintWriter out = response.getWriter();

out.println("<HTML><BODY>");
out.println("Test");
out.println("</BODY></HTML>");
}
}



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
 
T Mishra
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ganesh,

Please use code tags for any code you post. Its easy to read.
Try adding fully qualified class name for TemplateServlet.
 
Divya Chandel
Ranch Hand
Posts: 47
MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

here in action, servlet name is test.

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

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.

Bill
 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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.
 
K. Tsang
Bartender
Posts: 3443
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
k. Tsang ya i will try it now thank you
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

ROOT/WEB-INF/classes/my/spot/TemplateServlet.class

Bill
(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)

 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ William Thanks a lot for help. I will try with this
 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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">

<servlet>
<servlet-name>Y</servlet-name>
<servlet-class>zeeshan.Prog2</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Y</servlet-name>
<url-pattern>/P2</url-pattern>
</servlet-mapping>

</web-app>


that's it it ran I will figure out soon It was really problem of Tomcat version or content of my web.xml file
 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
K. Tsang
Bartender
Posts: 3443
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic