You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Stefan Görling <st...@pricerunner.com> on 2004/02/09 08:17:18 UTC

Configuring a Default Servlet

Hi,

I want to have all incomming request forwarded to a default servlet. 
Depending on the URL the request is forwarded to a JSP-page (too dynamic 
to use apache mod_rewrite).

The problem is that if I define the servlet as:
    <servlet-mapping>
        <servlet-name>custom</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

It will work, but when I do 
getDispatcher("MyPage.jsp").forward(request,response), it will forward 
to the servlet in a recursive way.

Is there any way to get a hold of a JSP-dispatcher when forwarding a 
request in order to do this?

Best Regards,

Stefan Görling



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Stealing the Writer (Was: Re: Configuring a Default Servlet)

Posted by Stefan Görling <st...@pricerunner.com>.
Dear Tim,

Thanks!, Filters works great to solve this problem. Here's another 
stupid thing I want to do. I want to measure the performance, by 
wrapping every request buy another filter (or similiar):

public void doFilter(ServletRequest request, ServletResponse, response, 
FilterChain chain) throws IOException, ServletException {
    ServletOutputStream out=response.getOutputStream();

    out.println("<!-- request started -->")
    chain.doFilter(request,response);
    out.println("<!-- This is the end of the request, it took foo 
seconds-->");

}

The page is delivered properly, but the debug output is not returned. If 
i remove chain.doFitlter, the text is printed properly. Anyone who has 
an idea on how to "Steal" a working writer to add some debug data at the 
end (start is not really necessary)?

/Stefan


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Configuring a Default Servlet

Posted by Tim Funk <fu...@joedog.org>.
Use filters.  They only get applied on the incoming request. In the filter - 
you can place the logic to perform the extra logic you need or not.


-Tim

Stefan Görling wrote:

> Dear Tim and others,
> 
> Thank you for the reference. And now for the little twist which I forgot 
> to mention:
> Before we introduced the default servlet we had urls with normal .jsp:s. 
> We want to run those through the custom dispatcher as well in order to 
> redirect to the proper url
> 
> For example:
> A request to: /Foo.jsp -> Permanent redirect to /foo/bar
> A request to: /foo/bar --> Internal forward to www/Foo.jsp
> A request to /Bar.jsp -->Internal forward to www/Bar.jsp
> 
> The problem is to say that all requests, including .jsp-files in the 
> root should be handled by the Servlet, but _not_ .jsp-files residing in 
> /www/
> 
> The following won't work: (neither with /www nor with /www/*)
>    <servlet-mapping>
>        <servlet-name>jsp</servlet-name>
>        <url-pattern>/www*</url-pattern>
>    </servlet-mapping>
> 
>    <servlet-mapping>
>        <servlet-name>custom</servlet-name>
>        <url-pattern>/</url-pattern>
>    </servlet-mapping>
> 
> As the getRequestDispatcher("/www/Foo.jsp").forward(request,response), 
> creates a loop.
> 
> So:
> 
> Is it possible to:
> a) configure web.xml in a proper way
> b) get a JSP-dispatcher, like 
> getJSPRequestDispatcher("/www/Foo.jsp").forward(request,response) 
> (tomcat specific typecasting is ok).
> c)  Rewrite the incoming RequestURI in a proper way?
> I've written code for :
> 1. in apache using mod_rewrite to change all url:s so that they begin 
> with /custom/foo/bar,
> 2. In the Servlet, wrap the request by extending the  
> HttpServletRequestWrapper and overriding getRequestURI() so that  
> /custom is removed.
> 
> This however gave a problem when trying to do a response.redirect to a 
> relative URL, as tomcat used an internal representation of the Request 
> object to retrieve the path information when converting the relative 
> path to an aboslute one, hence giving back a /custom/foo/bar url. I can 
> wrap the HttpServletResponse too to fix the redirect object, but I have 
> no idea when the same problem will appear somewhere else.
> 
> Best Regards,
> 
> Stefan Görling
> 
>> The defualt servlet is mapped by the following:
>>
>> SRV.11.2 Specification of Mappings
>> "A string containing only the ’/’ character indicates the "default" 
>> servlet of the application. In this case the servlet path is the 
>> request URI minus the context path and the path info is null."
>>
>> So you really want:
>>   <url-pattern>/</url-pattern>
>>
>> Also that same section states the /* has higher precendence than 
>> *.extension
>>
>> -Tim
>>
>> Stefan Görling wrote:
>>
>>> Hi,
>>>
>>> I want to have all incomming request forwarded to a default servlet. 
>>> Depending on the URL the request is forwarded to a JSP-page (too 
>>> dynamic to use apache mod_rewrite).
>>>
>>> The problem is that if I define the servlet as:
>>>    <servlet-mapping>
>>>        <servlet-name>custom</servlet-name>
>>>        <url-pattern>/*</url-pattern>
>>>    </servlet-mapping>
>>>
>>> It will work, but when I do 
>>> getDispatcher("MyPage.jsp").forward(request,response), it will 
>>> forward to the servlet in a recursive way.
>>>
>>> Is there any way to get a hold of a JSP-dispatcher when forwarding a 
>>> request in order to do this?



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Configuring a Default Servlet

Posted by Stefan Görling <st...@pricerunner.com>.
Dear Tim and others,

Thank you for the reference. And now for the little twist which I forgot 
to mention:
Before we introduced the default servlet we had urls with normal .jsp:s. 
We want to run those through the custom dispatcher as well in order to 
redirect to the proper url

For example:
A request to: /Foo.jsp -> Permanent redirect to /foo/bar
A request to: /foo/bar --> Internal forward to www/Foo.jsp
A request to /Bar.jsp -->Internal forward to www/Bar.jsp

The problem is to say that all requests, including .jsp-files in the 
root should be handled by the Servlet, but _not_ .jsp-files residing in 
/www/

The following won't work: (neither with /www nor with /www/*)
    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>/www*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>custom</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

As the getRequestDispatcher("/www/Foo.jsp").forward(request,response), 
creates a loop.

So:

Is it possible to:
a) configure web.xml in a proper way
b) get a JSP-dispatcher, like 
getJSPRequestDispatcher("/www/Foo.jsp").forward(request,response) 
(tomcat specific typecasting is ok).
c)  Rewrite the incoming RequestURI in a proper way?
I've written code for :
1. in apache using mod_rewrite to change all url:s so that they begin 
with /custom/foo/bar,
2. In the Servlet, wrap the request by extending the  
HttpServletRequestWrapper and overriding getRequestURI() so that  
/custom is removed.

This however gave a problem when trying to do a response.redirect to a 
relative URL, as tomcat used an internal representation of the Request 
object to retrieve the path information when converting the relative 
path to an aboslute one, hence giving back a /custom/foo/bar url. I can 
wrap the HttpServletResponse too to fix the redirect object, but I have 
no idea when the same problem will appear somewhere else.

Best Regards,

Stefan Görling

> The defualt servlet is mapped by the following:
>
> SRV.11.2 Specification of Mappings
> "A string containing only the ’/’ character indicates the "default" 
> servlet of the application. In this case the servlet path is the 
> request URI minus the context path and the path info is null."
>
> So you really want:
>   <url-pattern>/</url-pattern>
>
> Also that same section states the /* has higher precendence than 
> *.extension
>
> -Tim
>
> Stefan Görling wrote:
>
>> Hi,
>>
>> I want to have all incomming request forwarded to a default servlet. 
>> Depending on the URL the request is forwarded to a JSP-page (too 
>> dynamic to use apache mod_rewrite).
>>
>> The problem is that if I define the servlet as:
>>    <servlet-mapping>
>>        <servlet-name>custom</servlet-name>
>>        <url-pattern>/*</url-pattern>
>>    </servlet-mapping>
>>
>> It will work, but when I do 
>> getDispatcher("MyPage.jsp").forward(request,response), it will 
>> forward to the servlet in a recursive way.
>>
>> Is there any way to get a hold of a JSP-dispatcher when forwarding a 
>> request in order to do this?
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Configuring a Default Servlet

Posted by Tim Funk <fu...@joedog.org>.
The defualt servlet is mapped by the following:

SRV.11.2 Specification of Mappings
"A string containing only the ’/’ character indicates the "default" servlet 
of the application. In this case the servlet path is the request URI minus 
the context path and the path info is null."

So you really want:
   <url-pattern>/</url-pattern>

Also that same section states the /* has higher precendence than *.extension

-Tim

Stefan Görling wrote:
> Hi,
> 
> I want to have all incomming request forwarded to a default servlet. 
> Depending on the URL the request is forwarded to a JSP-page (too dynamic 
> to use apache mod_rewrite).
> 
> The problem is that if I define the servlet as:
>    <servlet-mapping>
>        <servlet-name>custom</servlet-name>
>        <url-pattern>/*</url-pattern>
>    </servlet-mapping>
> 
> It will work, but when I do 
> getDispatcher("MyPage.jsp").forward(request,response), it will forward 
> to the servlet in a recursive way.
> 
> Is there any way to get a hold of a JSP-dispatcher when forwarding a 
> request in order to do this?
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org