Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling servlet

 
Naveed Ali
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!

I successfully load the application(starting file = index.html) when i run tomcat 4 and enter the Url. But i am unable to call the servelt from index.html. What should be the solutions in following?

- Calling from a hyperlink.
- Can i call it by using 'Form' or something like 'Action' from html file.
- Is servlet-mapping necessary in above two cases. If yes then should i call the actual class or mapping class name.Plz insert a line or two of html code as an example.

Assuming the classes are in directory WEB-INF/Classes/Classes/

I have tried in various patterns but it returns error no. 404 while i can successfully load and run the servlets if i call it directly from Url wihtout using index.html.

Thnx in advance.
Naveed
 
asirob civokviz
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well,
when calling servlet use
./servlet/servletpackage.servletname

for example if your servlet is in sun.com package
and its name is ExampleServlet
you should call it
./servlet/sun.com.ExampleServlet

all servlet classes should be placed in web-inf/classes/
directory
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/servlet/servletpackage.servletname

No no no no - do NOT use the invoker servlet (implied by /servlet/) - it isn't even turned on in Tomcat 4 on. See the invoker servlet FAQ here at the ranchhouse.
Nothing but confusion can come of relying on the invoker servlet to address a servlet.
Bill
 
Ravikumar Jambunathan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming the classes are in directory WEB-INF/Classes/Classes/


First of all you must store the class files under WEB-INF/classes and try invoking the servlet using any of the standard procedure.

Secondy, according to your WEB-INF directory structure the Classes directory under /Classes will be treated as a package path.

for example,

package Example;

{
your class definitions
}

the dir structure should be WEB-INF/classes/Example/<your class file> and you must invoke the servlet using the package name.servletname.

the dd format is as follws.

<servlet>
<servlet-name>example</servlet-name>
<servlet-class>ExampleServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>example<servlet-name>
<url-pattern>/servlet/ExampleServlet</url-pattern>
</servlet-mapping>

hope this helps.

regards,
ravi.
[ June 15, 2004: Message edited by: Ravikumar Jambunathan ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you point to a good doc on the xml configuration you showed? I've seen several examples and have one or two up & running in Tomcat/Eclipse but the entries don't make sense to me yet. Thanks!
 
Ravikumar Jambunathan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi james,

hope you have misunderstood. I have given only the <servlet> and <servlet-mapping> portion as an example. I presume, you have misunderstood because of the sentence "dd format". I did not mean the web.xml format.

hope it is clear now.

regards,
ravi.
 
Naveed Ali
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, i have just missed one point that i have created new application by creating new directory 'application' under the 'webapps\ROOt\' dir. I have given the context path as '/application'. So the exact directory structure in my case is ROOT/application/classes/classes and not the previous one i had given. The WEB-INF dir is under the 'application' dir. I have packaged my servlet class in package named 'classes'. Here i am posting the mapping.

<servlet>
<servlet-name>Helloservlet2</servlet-name>
<servlet-class>classes.servlet2</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Helloservlet2<servlet-name>
<url-pattern>/classes/classes/Helloservlet2</url-pattern>
</servlet-mapping>

i had given '/classes/classes/Helloservlet2' in the hyperlink which gave me error 404.

regards

naveed
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, webapps\ROOT is already a web application - it has a WEB-INF and web.xml etc.
You can't create another web application under ROOT, you must create it under webapps. Assuming your application name is application and your servlet is in the "classes" package and the servlet class name is indeed "servlet2" like it says in your web.xml, your directory structure should be:


For documentation of how to fix your web.xml - why not go to the source?
http://java.sun.com/products/servlet/reference/api/index.html
and
http://java.sun.com/products/jsp/reference/api/index.html

Bill

[ June 15, 2004: Message edited by: William Brogden ]
[ June 15, 2004: Message edited by: William Brogden ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic