You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Bragg, Casey" <Ca...@allegiancetelecom.com> on 2000/11/30 19:43:51 UTC

Servlet in the middle... in the middle.. in the middle

Is there a way to do the following?

Let's say a user requests /foo/junk.gif, or /foo/test.jsp.

I'd like all requests to /foo/* to first be processed by a servlet.  
The servlet performs some operation (user authentication specifically).  
If the servlet processing performs normally, I want the request forwarded to
the page actually requested (in the case of .jsp files, the page must be
processed by the jsp engine first of course).

I've set my Tomcat 3.1 up as defined below.  The result is an infinite loop.
A request of /foo/junk.gif is redirected to the servlet and then to
/foo/junk.gif (which then is redirected to the servlet and then to
/foo/junk.gif... and so on).

Thanks so much for your help!

...Casey

---------------------------------------------------------------

Given a file at webapps/foo/junk.gif

And the following in server.xml : (Maps /foo urls to the middle_serv
context)
        <Context path="/foo" docBase="webapps/middle_serv" debug="0"
reloadable="true" ></Context

And the following in webapps/foo/web-inf/web.xml : (Maps *.gif to the
servlet middleServlet)
	<servlet>
		<servlet-name>
			middle_serv
		</servlet-name>
		<servlet-class>
			middleServlet
		</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>
			middle_serv
		</servlet-name>
		<url-pattern>
			*.gif
		</url-pattern>
	</servlet-mapping>

And a servlet called middleServlet: 
	public void doGet (HttpServletRequest request, HttpServletResponse
response) {
		try {
			// Perform user authentication.. on failure, go to
login page... otherwise proceed

			// Determine the originally requested URL and
forward the user to that page
			String orig_url = request.getRequestURI();
	
getServletConfig().getServletContext().getRequestDispatcher(orig_url).forwar
d(request, response);
		} catch (Exception ex) {
			ex.printStackTrace ();
		}	
	}

==============================================
Casey Bragg - Software Engineer
Allegiance Telecom, Inc.  Dallas, TX
214-261-8679 - casey.bragg@allegiancetelecom.com
==============================================


Re: Servlet in the middle... in the middle.. in the middle

Posted by Kedar Choudary <ke...@cysphere.com>.
I have rewritten following reply to MVC thread.
Hope it helps.
-----
guess, there IS a way to forward the .jsp processing to tomcat's
jsp-servlet, even when you have registered your own servlet to process all
.jps requests.

Instead of using
    ServletContext.getRequestDispatcher(url).forward(request, response);
use

ServletContext.getNamedRequestDispatcher("jserv-servlet").forward(request,
response);

It worked for me.

Note: This method does not work if you want to "re-write" the url in your
handler servlet. JSP files processed by the jserv-servlet is always the URL
of the original request.
-----------------------
Kedar Choudhary.

----- Original Message -----
From: Craig R. McClanahan <Cr...@eng.sun.com>
To: <to...@jakarta.apache.org>
Sent: Friday, December 01, 2000 1:23 AM
Subject: Re: Servlet in the middle... in the middle.. in the middle


> "Bragg, Casey" wrote:
>
> > Is there a way to do the following?
> >
> > Let's say a user requests /foo/junk.gif, or /foo/test.jsp.
> >
> > I'd like all requests to /foo/* to first be processed by a servlet.
> > The servlet performs some operation (user authentication specifically).
> > If the servlet processing performs normally, I want the request
forwarded to
> > the page actually requested (in the case of .jsp files, the page must be
> > processed by the jsp engine first of course).
> >
> > I've set my Tomcat 3.1 up as defined below.  The result is an infinite
loop.
> > A request of /foo/junk.gif is redirected to the servlet and then to
> > /foo/junk.gif (which then is redirected to the servlet and then to
> > /foo/junk.gif... and so on).
> >
>
> This question came up earlier today as well.  The bottom line is that you
cannot
> do what you are after in Tomcat 3.x (which is based on the servlet 2.2
> specification).  You can do it by using a Filter from the servlet 2.3 API
(which
> Tomcat 4.0 supports).  See my expanded explanation on a thread with
subject "MVC
> problem".
>
> >
> > Thanks so much for your help!
> >
> > ...Casey
> >
>
> Craig McClanahan
>
>


Re: Servlet in the middle... in the middle.. in the middle

Posted by "Vik P. Solem" <vs...@viktech.com>.
As I mentioned on the same thread "MVC problem" (after I had a few false
starts...) you can do your authentication in a single servlet and then
use <%@ page extend="MySecureServlet"%> in your pages so that they all
extend your servlet with the desired functionality.

-Vik

"Craig R. McClanahan" wrote:
> 
> "Bragg, Casey" wrote:
> 
> > Is there a way to do the following?
> >
> > Let's say a user requests /foo/junk.gif, or /foo/test.jsp.
> >
> > I'd like all requests to /foo/* to first be processed by a servlet.
> > The servlet performs some operation (user authentication specifically).
> > If the servlet processing performs normally, I want the request forwarded to
> > the page actually requested (in the case of .jsp files, the page must be
> > processed by the jsp engine first of course).
> >
> > I've set my Tomcat 3.1 up as defined below.  The result is an infinite loop.
> > A request of /foo/junk.gif is redirected to the servlet and then to
> > /foo/junk.gif (which then is redirected to the servlet and then to
> > /foo/junk.gif... and so on).
> >
> 
> This question came up earlier today as well.  The bottom line is that you cannot
> do what you are after in Tomcat 3.x (which is based on the servlet 2.2
> specification).  You can do it by using a Filter from the servlet 2.3 API (which
> Tomcat 4.0 supports).  See my expanded explanation on a thread with subject "MVC
> problem".
> 
> >
> > Thanks so much for your help!
> >
> > ...Casey
> >
> 
> Craig McClanahan

-- 
Vik P. Solem    vsolem@viktech.com    781-344-1133
VikTech Systems Inc.   Built right, now.
http://www.viktech.com

Re: Servlet in the middle... in the middle.. in the middle

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
"Bragg, Casey" wrote:

> Is there a way to do the following?
>
> Let's say a user requests /foo/junk.gif, or /foo/test.jsp.
>
> I'd like all requests to /foo/* to first be processed by a servlet.
> The servlet performs some operation (user authentication specifically).
> If the servlet processing performs normally, I want the request forwarded to
> the page actually requested (in the case of .jsp files, the page must be
> processed by the jsp engine first of course).
>
> I've set my Tomcat 3.1 up as defined below.  The result is an infinite loop.
> A request of /foo/junk.gif is redirected to the servlet and then to
> /foo/junk.gif (which then is redirected to the servlet and then to
> /foo/junk.gif... and so on).
>

This question came up earlier today as well.  The bottom line is that you cannot
do what you are after in Tomcat 3.x (which is based on the servlet 2.2
specification).  You can do it by using a Filter from the servlet 2.3 API (which
Tomcat 4.0 supports).  See my expanded explanation on a thread with subject "MVC
problem".

>
> Thanks so much for your help!
>
> ...Casey
>

Craig McClanahan