Re: mod rewrite, RequestDispatcher and intercepting requests to the server

2001-03-06 Thread Brett W. McCoy

On Tue, 6 Mar 2001, frederic barachant wrote:

 I also have the idea of doing this, and thought that we could make all accesses 
create a 404, simply
 by having an empty site) then, by having all of them going to a servlet, you would 
do the first part. (interception)
 By moving the http port of Apache to an other, we could, after rewriting, recreate 
the request, and
 send it directly to it. There would be no collision problem.
 The problem to achieve this is:
 How to redirect 404 to a single servlet?

You can use the error-page element in server.xml to map errors or
exceptions to the path of a resoruce in the web application.  See page 67
of the Servlet 2.2 spec.

-- Brett
http://www.chapelperilous.net/~bmccoy/

The Fifth Rule:
You have taken yourself too seriously.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]




Re: mod rewrite, RequestDispatcher and intercepting requests to the server

2001-03-06 Thread Mel Martinez


--- Michael Tickle [EMAIL PROTECTED]
wrote:
 I am attempting to intercept all requests to a
 server so I can extract
 information from the header.  I would then like to
 pass the request on to
 the server for it to display the user the requested
 page.

Sounds straightforward.

 
 I am led to believe that TomCat can not direct all
 requests to it on to a
 servlet - is this correct?  

Uh? What exactly are you trying to say here?  If you
are saying that tomcat can not be configured to send
every single request all to the same servlet, then I'm
not sure that's true.

Couldn't you setup servlet pattern-mapping like so:

servlet-mapping
servlet-name
DelagatorServlet
/servlet-name
url-pattern
/
/url-pattern
/servlet-mapping
servlet-mapping
servlet-name
DelagatorServlet
/servlet-name
url-pattern
*
/url-pattern
/servlet-mapping

You may need a couple of other patterns like "/*.*" or
"/*/*" as I'm not sure of the exact behavior of the
wild-card algorithm used in TC.

You may also want to define your own context for
"webapps/" to intercept activity there from going to
webapps/ROOT instead of to your application context.

I won't claim that this is simpler or better than just
using Apache's rewrite module.  It is recommended you
use apache anyway for serving static content, so it
may be simplest to just settup some RewriteRules to do
this (funnel all requests to a single servlet).

 I have installed apache and using mod rewrite all
 requests are forwarded on
 to my servlet.  The servlet the extracts the header
 information.  The
 problem then is showing the user the requested page.
  Ideally I would just
 like to return the request to the server and let it
 deal with it. I am told
 the closest to this is RequestDispatcher.  However
 RequestDispatcher seems
 only to be able to use relative linking - this will
 not work for me as I
 want to go from
 http://win2k:9090/servlet/myservlet?To=/index.html
 to
 http://win2k/index.htm
 
 Firstly is there a better way of doing what I am
 trying to do?

You want to use the HttpServletResponse.sendRedirect()
method instead of RequestDispatcher.forward().  The
forward() method only accesses urls reachable in the
current servlet engine thus they must be relative URLs
or 'absolute' relative to the current servlet context
path.  The sendRedirect() method is an actual HTTP
redirect that tells the browser to go somewhere else.

Keep in mind that neither a forward() or a
sendRedirect() will work once the response has been
committed, which can happen if you modify request
headers or write more output to the response than the
buffer size (8k I believe).

 
 Secondly is there a similar feature to
 RequestDispatcher that can use full
 URLs not relative URIs?
 

HttpServletResponse.sendRedirect()


 Thirdly has TomCat got any rewrite functionality (CF
 mod rewrite) since if I
 can not pass the request back to the apache domain I
 will have to somehow
 rewrite all the URLs in the tomcat domain.
 

TOmcat has pretty minimal rewrite capability
(basically the servlet-naming tag above).  This sounds
like a prime candidate for an Interceptor for a future
version (ideally it work consistently with apache
mod_rewrite rules in effect) but is not trivial and
probably won't happen real soon.

However, as indicated above, you should be able to
solve your problem by using sendRedirect() to send the
requests back to apache (or wherever).

Cheers,

Mel


__
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]




Re: mod rewrite, RequestDispatcher and intercepting requests...

2001-03-06 Thread Michael Tickle

In response to Mel Martinez...

Basically what I am trying to do is build a web counter that is session
aware.  I want to write a log file with request page, referring page, date,
time, user agent and session id.
I am currently using apache to sent all requests to my servlet with

RewriteRule ^(.*)/(.*) http://win2k:9090/servlet/redirect?To=/$2 [R,L]

so my servlet gets the parameter To and knows what page the user requested.
There are only two problems with my implementation so far (aside from the
fact I have not started the session tracking) first is serving the page to
the visitor.  I have managed this with requestdispatcher but that caused the
second problem tomcat does not then rewrite the links.

I just tried
response.sendRedirect("http://win2k/index.html");
but that does not work with my rewrite rule as the page is redirected back
to the servlet.  I was informed that this would not happen with
RequestDispatcher.

I am about to look in to the servlet mapping, but would that allow me to
preserve the identity of the requested page like the apache rewrite rule
does?

Could I possibly use HttpUtils() getRequestURL(HttpServletRequest req) to do
what I am after?  Failing that I am thinking of manually opening the file
and writing it out to the visitor - would that work??

The other option I can think of is to turn all the HTML pages in to JSP and
have the JSP collect the information (request page, referring page, date,
time, user agent and session id) and send it to a servlet - would that be a
better way of doing it?

Thanks for any help

Mike

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

Michael Tickle
Computing Science
Dept. of Computation

ICQ 5252 8934
Mob: 0777 968 5548
Fax: 0870 705 8382


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]




Re: mod rewrite, RequestDispatcher and intercepting requests...

2001-03-06 Thread Mel Martinez


--- Michael Tickle [EMAIL PROTECTED]
wrote:
 I am currently using apache to sent all requests to
 my servlet with
 
 RewriteRule ^(.*)/(.*)
 http://win2k:9090/servlet/redirect?To=/$2 [R,L]
 
 so my servlet gets the parameter To and knows what
 page the user requested.

That needs to have some more 'smarts' added to it.

 
 I just tried
 response.sendRedirect("http://win2k/index.html");
 but that does not work with my rewrite rule as the
 page is redirected back
 to the servlet.  

Of course it won't!  Your rewrite rule is immediately
forwarding it 'back' to the servlet! :-)

1) request comes to apache
2) your rewrite rule sends it to tomcat servlet
3) tomcat servlet redirects it to apache
4) repeat at (2) !!!

You need to put some clue to the Rewrite Engine to
tell it that you don't want a URL to be rewritten
further.

I would suggest that you preprocess the url in your
servlet before you do the sendRedirect to embed some
clue such as rewriting it as:

http://win2k/index.html -
http://win2k/index.html?DELEGATED=true

or whatever.  Next, modify your rewrite rule to have a
conditional:

RewriteCond  $0  !.*/DELEGATED.*
RewriteRule ^(.*)/(.*)
http://win2k:9090/servlet/redirect?To=/$2 [R,L]

This will make your rule skip any URL with the 'clue'
you've embedded.
Next, apply a rule to remove the 'clue'

RewriteCond $0 .*/DELEGATED.*
RewriteRule (.*)/DELEGATED(.*) $1$2

don't redirect this time or you'll just jump back into
the cycle.

Alternatively, you might just add a query string param
or set some other request info to act as your 'clue'.

Still another alternative is to simply run another
Apache server instance or virtual host that does NOT
apply your rewrite rule.  There are lots of
strategies.  Use your imagination.  :-)

I was informed that this would not
 happen with
 RequestDispatcher.


??? RequestDispatcher will only forward or include
requests to be serviced in the same servlet engine. 
It can not redirect back to apache.
 
 The other option I can think of is to turn all the
 HTML pages in to JSP and
 have the JSP collect the information (request page,
 referring page, date,
 time, user agent and session id) and send it to a
 servlet - would that be a
 better way of doing it?
 

It depends on your application.  I don't really have a
clear idea on what your high-level goal is here.

Good luck,

mel


__
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]