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 Servlet path stupid question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet path stupid question" Watch "Servlet path stupid question" New topic
Author

Servlet path stupid question

Vikas Aggarwal
Ranch Hand

Joined: Jun 22, 2001
Posts: 140
Hi,

My stupid question is:

I always see my url to a servlet like:

http://localhost/servlet/test

Is it possible to have my url something like this:

http://localhost/test

or

http://localhost/<something>/test

Is there any simple way to do this like in web.xml? Besides that I would still want my http://localhost/servlet/test to be valid.

Thanks.

Regards,
Vikas
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
hello vikas,

The only stupid question is the one that is never asked. Its a very good question and I don't know the answer myself correctly.

I may be wrong but this is what I think of this situation,
if you are mapping "/test/*" this would mean any url which has /test/ context. So it maps both "/servlet/test/" and "/test" to this mapping.

We shall wait for any expert to answer this corretly though.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

It's all in your web.xml servlet-mapping tag.



Now, your servlet should be available under all three requests.


GenRocket - A Test Data Generation Platform
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
Originally posted by Gregg Bolinger:
It's all in your web.xml servlet-mapping tag.



Now, your servlet should be available under all three requests.


I knew I was wrong. Thanks for the correct answer.
Vikas Aggarwal
Ranch Hand

Joined: Jun 22, 2001
Posts: 140
Thanks for the answer. Now I have got a problem with this solution.

I have around 536 servlets in my app and it is impossible or rather time consuming and error prone to list all of them in web.xml. Is there a way to make this work for all servlets.

Thanks.
Vikas
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
536 servlets , yeah good luck with that buddy
Vikas Aggarwal
Ranch Hand

Joined: Jun 22, 2001
Posts: 140
Yes it is an old app with no luck at all. So is it possible to do something about it?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Yes and no. Don't you love that answer? You need to consider using the Front Conroller pattern. Basically, you need a single servlet that is mapped in your web.xml and that servlet will parse the request and foward the process to a particular "action" that handles the request. This is basically what frameworks like Struts do. There is tons of discussion on Front Controller including this nifty little article written by our very own Bear.

http://www.javaranch.com/journal/200603/Journal200603.jsp#a5
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Originally posted by Vikas Aggarwal:
Yes it is an old app with no luck at all. So is it possible to do something about it?


If you are dealing the legacy code and you can't utalize a Front Controller, then you are out of luck. You have to specify those mappings to get the desired results.
Vikas Aggarwal
Ranch Hand

Joined: Jun 22, 2001
Posts: 140
To be honest, I completely loved your answer. Front controller is a good idea. I will see its feasibility in my project.

Thanks a lot for your help.

Vikas
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Thanks for the plug Gregg!

Yes, the very problem of "deployment descriptor bloat" is discussed in that article.

A Front Controller can be very simple and light-weight -- you don't need a behemoth like Struts just to implement one.

The article touches the high level design of such a controller but doesn't go into great detail. My next planned article will be on the detailed design of a Front Controller but you probably can't wait around for that.
[ October 11, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet path stupid question
 
Similar Threads
sendRedirect
Servlet Redirection
Welcome file
Running Simple Servlets
tomcat4