If I understand correctly chain.doFilter(request, wrapper); is basically managing the forwarding behavior your looking to accomplish during the decision making process (IE to execute the servlet, jsp, or return static content via the default servlet.
In otherwords, if the request matches a specific pattern, then pass it onto the next filter, eventually this will hit the default or jsp servlets for you without your needing to resolve the servlet that will be at the end.
In instances where you recognize in the regexp that you don't want this behavior your going to need logic to determine what the alternate behavior would be.
In terms of aquiring the default servlet:
Speculation: the RequestDispatcher may be doing "mapping" internally too, so you may need to be doing something like the following in your web.xml to actually aquire a request dispatcher directly to the servlet.
<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>blaa</url-pattern> </servlet-mapping>
RequestDispatcher defaultDispatcher = getServletContext().getNamedDispatcher("blaa"); defaultDispatcher.forward(request, response);
But this is just speculation.
-Mark
Albert Moliner wrote:
Touché. I don't know why I had not thought of filters for this...
Oh, yes, now I know: My initial intention was to return the JSP file contents as if it were a .txt file, and though ServletContext.getResourceAsStream could be used, it involved having to implement the same behaviour as the default servlet does (last modified checks, etc.). So after all I wanted the default servlet to serve the request, and the filter would have needed the same behaviour as the forwarding servlet I had implemented.
Therefore, my question now becomes: If I needed the default servlet (or the jsp servlet, as it seems to have the same problems) to serve all requests inside a subdirectory, or perhaps just some files with a pattern under a subdir, how should I proceed?
In other words: What's wrong with doing (don't consider code quality, it's just to show the procedure)
---- RequestDispatcher defaultDispatcher = getServletContext().getNamedDispatcher("default"); defaultDispatcher.forward(request, response); ----
? Why doesn't it work (and "jsp" doesn't, either), while any other servlet name just seems to work fine?
Or, otherwise, in the app's web.xml,
---- <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/foo/bar/*</url-pattern> </servlet-mapping> ----
? Amazingly,
---- <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/foo/bar/file.jsp</url-pattern> </servlet-mapping> ----
does seem to work, while not the "/foo/bar/*" mapping (and this mapping works with any other servlet name defined in the same web.xml file).
Is it that today it's a Friday? Am I misunderstanding something very basic? Is this a common behaviour, or just happens in Tomcat (4.1.something)?
Thanks, Albert.
----- Original Message ----- From: "Mark R. Diggory" <[EMAIL PROTECTED]> To: "Tomcat Users List" <[EMAIL PROTECTED]> Sent: Friday, December 12, 2003 4:24 PM Subject: Re: Disabling JSP execution under certain dirs
Yes, Unfortunately, I bounced off this as well, it would have behooved the Servlet API developers to allow URL Rewrites to be a little more powerful from a REGEXP standpoint. Look at the j2sdk1.4 api, we now have regexp's available there default. One would suspect that the Servlet API could easily start allowing more complex regexp into these entries.
I would recommend writing a Filter that gets run prior to all jsp's and applying your own regexp/forwarding to the jsp's when you deem it appropriate. This requires j2sdk 1.4 and is pseudo code.
public final class BlockJspFilter implements Filter {
String regexp = "*.(/foo/bar.jsp|/bim/bam.jsp).*";
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if(request.getRequestURL().matches(regexp)){ /*send an error response*/ else{ chain.doFilter(request, wrapper); } ... } }
<filter> <filter-name>BlockJspFilter</filter-name> <filter-class>BlockJspFilter</filter-class> </filter> <filter-mapping> <filter-name>BlockJspFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>
One could even go as far as to use the FilterConfig to pass in the regexps which could then be dynamically adjusted in the web.xml
http://java.sun.com/products/servlet/Filters.html
-Mark Diggory
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
-- Mark Diggory Software Developer Harvard MIT Data Center http://osprey.hmdc.harvard.edu
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]