wood burning stoves 2.0*
The moose likes Servlets and the fly likes Cannot invoke servlet by name! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Cannot invoke servlet by name!" Watch "Cannot invoke servlet by name!" New topic
Author

Cannot invoke servlet by name!

Goran Markovic
Ranch Hand

Joined: Sep 26, 2008
Posts: 399
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

Joined: Dec 11, 2004
Posts: 13410

Your url-pattern is /hello.html but you're trying to hit it with
http://localhost:8080/mywebapp/hi

try

http://localhost:8080/mywebapp/hello.html


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 10, 2002
Posts: 60780
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Goran Markovic
Ranch Hand

Joined: Sep 26, 2008
Posts: 399
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

Joined: Sep 26, 2008
Posts: 399
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

Joined: Oct 26, 2006
Posts: 218
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

Joined: Sep 26, 2008
Posts: 399
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

Joined: Oct 26, 2006
Posts: 218
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

Joined: Sep 26, 2008
Posts: 399
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Sep 26, 2008
Posts: 399
Well, however thanks to all for such a comprehensive discussion!

See ya around...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot invoke servlet by name!
 
Similar Threads
web.xml within tomcat
Basic Auth login box
cannot invoke servlet in Tomcat
getting exception in JSF Tutorial of JR
Running Servlets, have a question