Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot invoke servlet by name!

 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a problem when I want to invoke servlet by name?
I've simply mapped it in my web.xml. I can invoke it by class name or with any kind of mapping (Explicit mappings,Path prefix mappings,Extension mappings), but just to me, it won't by name. Here a simple which won't work at me:




When I call - http://localhost:8080/mywebapp/hi , I get the famous 404 Error. Does anyone know what the problem is???

[ November 19, 2008: Message edited by: Slobodan Erakovic ]
[ November 19, 2008: Message edited by: Slobodan Erakovic ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you should package all of your Java classes.

Your server might be able to find an un-packaged servlet but you will run into problems later (trying to locate bean classes from JSPs, for instance) if you continue using unpackaged Java classes in a web application.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's also a really really really bad idea to use a mapping that ends with .html.

The browsers, proxy servers, gateways and other resources on the net will assume that its a statis resource and you could run into caching issues.
 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't I be able to invoke a servlet by registered name??? That is the question. I put my servlet in package, and I can make call to him by class name, or by (as I wrote) any sort of registered url (</url-pattern> . The reason why I put /index.html, is because of experimenting. But a problem is about invoking by registered name. I correct package name and still nothing...





Furthermore, I cannot at all invoke my servlet now (when I put it in the boby package)! The "boby" folder, with HelloWorld class is stored in "classes" folder, just as it should. But now I cannot call it neither by name, nor by class, nor by url--pattern. What is wrong now???
[ November 20, 2008: Message edited by: Slobodan Erakovic ]
 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, really guys, I cannot inovke it at all... I registered it as :



then I try as :




without backslash, but it wont work... Neither with uri-pattern registration (either like : /hello.tml, /hello/*, or by extension!!) Nothing works!!! But, it stop working when I put it in the package so it must be a bad sort mapping....help....
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the problem:
<servlet-class>
boby/HelloWorld
</servlet-class>


it should be
<servlet-class>
boby.HelloWorld
</servlet-class>


assuming you class looks like this



Also as pointed out by Bear earlier change your mapping's extension from .html

make it something like this

<url-pattern>/hello.do</url-pattern>
 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it's work now but Unfortunately you also forget to tell me :?Can I call the servlet by it's registered name, and if I can, how"???
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want to invoke it as
http://localhost:8080/mywebapp/hi ?

Simply try changing the mapping in url-pattern as

<url-pattern>
/hi
</url-pattern>
 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha, so I cannot perform invocation by registereg name of servlet... That's what I think so, because, despite my intention to do such a thing I couldn't. Thanks!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing in the spec about being able to invoke a servlet by its name.

There was a period when containers had a mechanism to allow servlets to be invoked by a fully qualified class name for development and testing but, for security reasons, most containers have removed this feature.
For Tomcat this was done with the InvokerServlet.

See this to see why this is no longer active by default in Tomcat:
http://faq.javaranch.com/java/InvokerServlet

Most other containers have taken similar measures.
 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, however thanks to all for such a comprehensive discussion!

See ya around...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic