Servlet redirection but maintain url in browser (make sense?!?)

Hi,

Would like to have a url like www.foo.com/careers

which will call a Servlet, passing in a parameter of "careers" so I know what content to display.

But instead of the url changing to www.foo.com/Servlet?page=careers in the browser (due to obj.con redirect), is there a way I can do this without the url changing from www.foo.com/careers, but still calls the relevant servlet and passes in the correct parameter?

Thanks

[453 byte] By [garethka] at [2007-11-27 10:57:55]
# 1

We were covering a very similar topic in this thread:

http://forum.java.sun.com/thread.jspa?threadID=5194280&tstart=10

Ultimately you would use the If structure with regular expressions to restart the request internally (the user-agent does not see this) with the full URI. Something like:

<If $uri =~ "/(.*)"

and $uri !~ "^/Servlet.*" >

NameTrans fn="restart" uri="/Servlet?page=$1"

</If>

This block appropriately placed in your obj.conf will internally restart the request with the original URI now used as the query string of the new request.

You could also use this instead:

<If not $internal

and $uri =~ "/(.*)" >

This will only perform the restart if the request being processed is determined to NOT be internally generated (e.g. an already restarted request).

Of course you should make sure that you application generates URLs that are appropriate to this URI space, not the canonical URI space. This configuration will also be pretty greedy with your URI space, preferring to rewrite everything:

/foobar/sub/dir/image.jpg

becomes

/Servlet?foobar/sub/dir/image.jpg

So you'll want to refine what options you're willing to match, and restrict the expression used here to be less inclusive.

For more information on this very nifty feature, see the docs at:

http://docs.sun.com/app/docs/doc/820-1062/6ncoqnpfh?a=view

JoeMcCabea at 2007-7-29 12:12:14 > top of Java-index,Web & Directory Servers,Web Servers...
# 2

Thanks for the reply Joe.

Am I right in saying this is only available from Version 7 on? I am currently still running 6.1 unfortunately

garethka at 2007-7-29 12:12:14 > top of Java-index,Web & Directory Servers,Web Servers...
# 3

The example in Joe's post is for Web Server 7. Web Server 7's URL manipulation/rewriting capabilities are far superior to 6.1.

Do you want your servlet to take over handling all requests?

i.e

<servlet-mapping>

<servlet-name>

myservlet

</servlet-name>

<url-pattern>

/*

</url-pattern>

</servlet-mapping>

If yes, then request.getPathInfo() will return /careers (for a request for /careers).

Arvind_Srinivasana at 2007-7-29 12:12:14 > top of Java-index,Web & Directory Servers,Web Servers...
# 4

Hi.

I dont want the servlet to handle all requests, as I may have a few servlets, but also a few static html files etc.

I have this in my web.xml file:

<servlet-mapping>

<servlet-name>GenericServlet</servlet-name>

<url-pattern>/Generic</url-pattern>

</servlet-mapping>

so if a user goes to http://server/Generic?page=careers, the servlet will read a file called careers.html, and forward to a jsp.

But what I want to do is be able to have a url that looks like http://server/careers which will call the above url, but not change the url in the users browser.

I tried using a redirect:

NameTrans fn="redirect" from="/careers" url="http://server/Generic?page=careers"

but the url changes in the browser.

garethka at 2007-7-29 12:12:14 > top of Java-index,Web & Directory Servers,Web Servers...
# 5

Try using "restart" instead of "redirect".

e.g., NameTrans fn="restart" from="/careers" uri="/Generic?page=careers"

Seema.Aa at 2007-7-29 12:12:14 > top of Java-index,Web & Directory Servers,Web Servers...
# 6

restart function not available in Sun One Webserver 6.1. Any other ideas?

garethka at 2007-7-29 12:12:14 > top of Java-index,Web & Directory Servers,Web Servers...
# 7

A relatively simple NSAPI could be created that would internally rewrite the URI in the request pblock, but if you plan to have many plans to do URI/URL manipulation in a relatively straightforward way you should seriously consider upgrading to Web Server 7.0.

JoeMcCabea at 2007-7-29 12:12:14 > top of Java-index,Web & Directory Servers,Web Servers...