wood burning stoves 2.0*
The moose likes Servlets and the fly likes Tomcat 5.0.28 and servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Tomcat 5.0.28 and servlets" Watch "Tomcat 5.0.28 and servlets" New topic
Author

Tomcat 5.0.28 and servlets

Mohammad Farhat
Ranch Hand

Joined: Nov 17, 2005
Posts: 51
Hey guys,
this is the first time i try servlets, and i've been having problems testing them on my system
i have tomcat 5.0.28 installed, i was able to write a small servlet test class (HelloWorld.java), compiled it, and placed it inside the WEB-INF/classes
i have also modified the web.xml file to contain the following:
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
but still the browser doesn't seem to recognize it, i'm still getting that old 404
Ideas?
thanks
Mohammad
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61456
    
  67

All classes must be in a package other than the default.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mohammad Farhat
Ranch Hand

Joined: Nov 17, 2005
Posts: 51
thanks for the fast reply Bear,
actually, all i have is a single class, and it's packageless, do i have to place it in a package ?
thanks
Mohammad
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61456
    
  67

All classes must be in a package other than the default.
Mohammad Farhat
Ranch Hand

Joined: Nov 17, 2005
Posts: 51
Well
i added a package to the class, and still didn't work (let's say package dd)
so now i am calling http://localhost:8080/servlet/dd.HelloWorld -> 404 again
do i need to modify any other files configs?
thanks
Mohammad
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61456
    
  67

What's up with the "/servlet" in your URL? That's not part of your mapping.
Mohammad Farhat
Ranch Hand

Joined: Nov 17, 2005
Posts: 51
mmm .. okay .. so i even tried without the servlet part, and didn't work (i got that from one site teaching about servlets, they said it is required to call using /servlet/)
can you give me a small checklist of what i should have so that the servlet can be called / will work correctly
really appreciated dude
Best,
Mohammad
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61456
    
  67

According to your web.xml mapping the url should be:

http://yourserver/contextPath/HelloServlet

where /contextPath is the context path to your web (the empty string if it's the root web app).

You might want to show us the new web.xml entries.
[ January 10, 2006: Message edited by: Bear Bibeault ]
kiranreddy reddy
Greenhorn

Joined: Jan 02, 2006
Posts: 11
After you put the servlet in a new package,

make sure your web.xml should contain the following

<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>packagename.HelloWorld</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

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

That is change the <servlet-class>
Now restart the tomcat server

and check in ur browser, http://localhost:8080/contextPath/HelloWorld

Make sure you type in the correct contextPath
Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
All classes must be in a package other than the default.


This is not mandatory.....but recommendation

I think problem will be with URL...

Shrinivas
Sarath PS
Greenhorn

Joined: Oct 22, 2004
Posts: 11
I remember some web application that used package-less classes, stopped working after being migrated to 5.0.28.
Package is a must for tomcat after 5.0.x , i believe.
Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Try it out!!! Best option available
Shrinivas
Mohammad Farhat
Ranch Hand

Joined: Nov 17, 2005
Posts: 51
Thanks guys for all the replies,
i re-did the whole configuration on another system, and i tried out calling the servlet as "http://localhost/contextPath/HelloWorld" and it worked,
i'm not sure whether it was something wrong with the old configuration on the other pc, or the new calling method, but anyhow thanks everyone
highly appreciated
Best,
Mohammad
Mohammad Farhat
Ranch Hand

Joined: Nov 17, 2005
Posts: 51
and btw, no need for a package name, it worked without including a default package for the class...
i'm not sure if this applies to all tomcat versions, but at least for this one (5.0.28), it is not required
again, thanks all
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You can get away without a package name for your servlets.
It's the beans that will give you problems.

The problem comes when you try to import a class belonging to the default package (non packaged class) from a class in a package other the default package. This is not a Tomcat issue, it's a tightning of the specs in JREs 1.4.0 and higher.

Since the JSP's generated servlet classes go into the org.apache.jsp package, they will not be able to import beans withouth an explicit package statement.

If this seems like a headache to sort out, it is.
Fortunately, there is no need to do so.
The solution is simple.
Put all of your classes in packages.
Unpackaged classes in anything but a trivial command line app is just poor code structuring.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
 
subject: Tomcat 5.0.28 and servlets