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 Trouble Loading my First Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Trouble Loading my First Servlet" Watch "Trouble Loading my First Servlet" New topic
Author

Trouble Loading my First Servlet

Nirmal Mukundan
Ranch Hand

Joined: Feb 21, 2007
Posts: 37

Hi All

I am a beginner to servlets and do require your help in running my servlet.
My doubt is do we require a servlet mapping tag for every servlet. For example in one of the books I have referring..they have created a servlet TestServlet which is the
package com.exampe.web.

They call the servlet by giving the URL: http://localhost:8080/LifeCycle/servlet/TestServlet/.
They haven't mentioned anything about the deployment descriptor.

I tried out the same example.I have created the same package structure and in my web.xml I have added

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.example.web.TestServlet</servlet-class>
</servlet>

</web-app>

I have placed the classes in the WEB-INF/classes folder of my root folder and the web.xml under WEB-INF

But still I am not able to list my servlet.

Is there anything missing. Please help me

Nirmal
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Yes, you must map the servlet in the deployment descriptor. If you are using an out-dated book, it's probably relying on the now-deprecated invoker.

You must also place all Servlets and other classes in a package other than the default.

Sounds like you need a more up-to-date reference.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nirmal Mukundan
Ranch Hand

Joined: Feb 21, 2007
Posts: 37

Hi Bear

Do i need to add the servlet mapping and sevlet tags in web.xml as


<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.example.web.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/servlet/TestServlet </url-pattern>
</servlet-mapping>

Nirmal
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

What happened when you tried it?
Nirmal Mukundan
Ranch Hand

Joined: Feb 21, 2007
Posts: 37

Hi

This is what i am getting

HTTP Status 404 - /LifeCycle/servlet/SpecialCharServlet/

type Status report

message /LifeCycle/servlet/SpecialCharServlet/

description The requested resource (/LifeCycle/servlet/SpecialCharServlet/) is not available.iv

and in the web.xml i have given it as

<servlet>
<servlet-name>SpecialCharServlet</servlet-name>
<servlet-class>com.example.web.SpecialCharacterServlet</servlet-class>
</servlet>


<servlet-mapping>
<servlet-name>SpecialCharServlet</servlet-name>
<url-pattern>/servlet/SpecialCharServlet</url-pattern>
</servlet-mapping>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

You went through the trouble to map it in the deployment descriptor, and then ignored the mapping.
Nirmal Mukundan
Ranch Hand

Joined: Feb 21, 2007
Posts: 37

Yes..I ignored the mapping first..then after giving it as mentioned below

<servlet>
<servlet-name>SpecialCharServlet</servlet-name>
<servlet-class>com.example.web.SpecialCharacterServlet</servlet-class>
</servlet>



<servlet-mapping>
<servlet-name>SpecialCharServlet</servlet-name>
<url-pattern>/servlet/SpecialCharServlet</url-pattern>
</servlet-mapping>


I call my servlet as http://localhost:8080/LifeCycle/servlet/SpecialCharServlet/

Still i get the error

type Status report

message /LifeCycle/servlet/SpecialCharServlet/

description The requested resource (/LifeCycle/servlet/SpecialCharServlet/) is not available.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

What's LifeCycle? Are you sure that it's your application's context path?

Also, have you tried it without the trailing "/"? Please show some effort on your part.
Nirmal Mukundan
Ranch Hand

Joined: Feb 21, 2007
Posts: 37

Yes Bear..LifeCycle is the root of my web application..
Nirmal Mukundan
Ranch Hand

Joined: Feb 21, 2007
Posts: 37

Thanks Bear..it working...

Actually i was stuck with this..So the doubt i have is if Im calling a servlet by the url

http://localhost:8080/LifeCycle/servlet/SpecialCharServlet

the url-pattern in my web.xml should start from /servlet/SpecialCharServlet. Is this the way..

Nirmal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble Loading my First Servlet
 
Similar Threads
trying to run hello world script but it is not
FAIL - Application at context path /pro1 could not be started
Unable to find taglib in project--please help
Unable to Run a Servlet in Apache Tomcat
the application is not deployed on tomcat in eclipse