Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tomcat 5.0.28 and servlets

 
Mohammad Farhat
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All classes must be in a package other than the default.
 
Mohammad Farhat
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All classes must be in a package other than the default.
 
Mohammad Farhat
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's up with the "/servlet" in your URL? That's not part of your mapping.
 
Mohammad Farhat
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it out!!! Best option available
Shrinivas
 
Mohammad Farhat
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic