aspose file tools*
The moose likes Servlets and the fly likes URL Rewriting in browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "URL Rewriting in browser" Watch "URL Rewriting in browser" New topic
Author

URL Rewriting in browser

Sohil Shah
Greenhorn

Joined: Dec 20, 2008
Posts: 5
Hi,

There is a web application hosted in tomcat.
It can be accessed through -
http://localhost:8080/MyApplicationContext

Now if I want to Access ant further java action, I access those through an ActionServlet which is written.

So the URL looks like

http://localhost:8080/MyApplicationContext/ActionServlet?MY_ACTION=DO_SOMETHING

This is how the current application works.
I now just want to rewrite the URL.

I will be given an extra word at runtime..e.g EXTRA_WORD

so if somebody tries to access the application using URL
http://localhost:8080/MyApplicationContext/EXTRA_WORD

Then that person should be able to access the same application.

Also, This EXTRA_WORD should remain in the URL throughout the application.

So any access to further actions should look like -

http://localhost:8080/MyApplicationContext/EXTRA_WORD/ActionServlet?MY_ACTION=DO_SOMETHING

Thanks,
Sohil
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Are you familiar with javax.servlet.Filter? Read on about them. This is a suitable Fitler task.
Sohil Shah
Greenhorn

Joined: Dec 20, 2008
Posts: 5
Hi Bauke,

Thanks for the reply.
I am using the filters only.
But can you please elaborate on what should I write in filters?

Actually, the thing is - I have created my application already. And it's working fine. The only thing I need to change is the URL the user sees. I just want to add an extra word in the URL.

Sohil.
Sohil Shah
Greenhorn

Joined: Dec 20, 2008
Posts: 5
Originally posted by Bauke Scholtz:
Are you familiar with javax.servlet.Filter? Read on about them. This is a suitable Fitler task.


Hi Bauke,

Thanks for the reply.
I am using the filters only.
But can you please elaborate on what should I write in filters?

Actually, the thing is - I have created my application already. And it's working fine. The only thing I need to change is the URL the user sees. I just want to add an extra word in the URL.

Sohil.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi sohil,

then, i think no need to go for filter. suppose you servlet mapping is /ACTION-SERVLET then change it to /EXTRA_WORDS/ACTION-SERVLET in web.xml
and then try. let see it helps you or not
Sohil Shah
Greenhorn

Joined: Dec 20, 2008
Posts: 5
Originally posted by seetharaman venkatasamy:
Hi sohil,

then, i think no need to go for filter. suppose you servlet mapping is /ACTION-SERVLET then change it to /EXTRA_WORDS/ACTION-SERVLET in web.xml
and then try. let see it helps you or not


Hi Seetharaman,

The thing is that EXTRA_WORD is decided at runtime. I have list of those allowed wo rds on an XML file.
So it can be
/EXTRA_WORD1/ACTION-SERVLET or
/EXTRA_WORD2/ACTION-SERVLET etc.

Also if I change the servlet mapping , I don't think will work for me. Because I will need to change all the URLs and href in my application which simply works for /ACTION-SERVLET mapping.
Uli Hofstoetter
Ranch Hand

Joined: Nov 24, 2006
Posts: 57
Maybe you should rethink your design wether the EXTRA_WORD should rather be a request parameter than a part of the URI.


SCEA5, Certified ScrumMaster
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Just implement the filter on the generic url pattern and let it read the same configuration file how to forward the 'pretty URL' to a 'real url'. If there is no such mapping then just let it continue the request transparently.
Sohil Shah
Greenhorn

Joined: Dec 20, 2008
Posts: 5
Originally posted by Bauke Scholtz:
Just implement the filter on the generic url pattern and let it read the same configuration file how to forward the 'pretty URL' to a 'real url'. If there is no such mapping then just let it continue the request transparently.


Hi Bauke,

The thing is that..The application is already been created and it's up and it works fine without that EXTRA_WORD thing. i.e all JSPs and JS file contains all form Actions, hrefs, and ajax call in the form of ActionServlet?My_Action=Somthing..
So if I now want to insert this EXTRA_WORD, i'll have to change all these JSPs and JS files.

Let me put the problem in more simpler way.
we have an application .. it's hosted on ..say..
www.application.com
their users are say CUST1 and CUST2 and many more...
Previously, the customer were given the generic URL that is..www.application.com

now we want to give them more personalized URL like www.application.com/CUST1
Also we'll show some CSS effects on the bases the customer specific URL(like their logo or something like that)

Appreciate your help.

Thanks,
Sohil
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285

sohil:
will be given an extra word at runtime..e.g EXTRA_WORD


You know that whatever action you are sending in request ,the web container will try to match it in web.xml to find a suitable match.
The point is, you should understand that web container is basically trying to do pattern matching.you have specified the Url-pattern for your servlet in the web.xml .

BUT you are trying to do the exact match.
There are three ways to give url pattern in the web.xml
1.Exact match
2.Directory match
3.Extension match

you should go with Directory match rather than exact match as your request action will change partially at runtime.
[ December 23, 2008: Message edited by: raj malhotra ]
 
Don't get me started about those stupid light bulbs.
 
subject: URL Rewriting in browser