You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by prt <sh...@pelephone.co.il> on 2007/03/06 16:58:15 UTC

Servlet Filtering & performance..

Hi to all again,

I added filter object for all requests  <url-pattern>/*</url-pattern>.
In the Filter class i check the ServletPath
(HttpServletRequest.getServletPath()) and
i check if is equals to alist of allowd paths. it is work just perfect.
But i think that is damage the performance, is it ?

Thank you.
-- 
View this message in context: http://www.nabble.com/Servlet-Filtering---performance..-tf3356429.html#a9334933
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: [OT] Servlet Filtering & performance..

Posted by prt <sh...@pelephone.co.il>.
I dont want to handle the display process, just to map it from the filter to
"D:\storeg\data\uf..".
But thank you for your solution.


Christopher Schultz-2 wrote:
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> prt,
> 
> prt wrote:
>> I have another problem,
> 
> You should post a separate message in this case.
> 
>> I save uploaded images(by users)  in folder place at
>> "D:\storeg\data\uf\".
>> So when the URL is "http://localhost/test/uf/xyz.jpg" then is redirect to
>> "D:\storeg\data\uf\xyz.jpg" 
> 
> I assume you mean "it is mapped to d:\..."... redirecting to a local
> file path won't work unless you are running your browser from the
> application server.
> 
>> And that so because i configure in server.xml this part between the host
>> tag,
>> <Context path="/uf" docBase="D:\storeg\data\uf" debug="0"
>> crossContext="false"/>
>> 
>> Is there way that i can do that by using the filter, how can i redirect
>> the
>> request from the filter when is "/uf..." to
>> "D:\storeg\data\uf..." with out loading the image and print it out from
>> the
>> filter ?
> 
> You have two options:
> 
> 1. Map a directory into your URL space (not sure how to do that in
>    Tomcat, but I'm sure that's been covered in the archives).
> 
> 2. Write an image-serving servlet. Something like this:
> 
> <servlet-mapping>
>    <url-pattern>/ug/*</url-pattern>
>    <servlet-name>imageServlet</servlet-name>
> </servlet-mapping>
> 
> Your servlet class, written in loose pseudocode:
> 
> doGet()
> {
>  String filename = request.getExtraPathInfo();
> 
>  // You should check for ".." and other evil-looking paths
> 
>  filename = "D:/storeg/data/ug/" + filename;
> 
>  File f = new File(filename);
> 
>  response.setContentType("whatever/is+appropriate");
>  response.setContentLength(f.length());
> 
>  FileInputStream in = new FileInputStream(filename);
>  OutputStream out = response.getOutputStream();
> 
>  byte[] buffer = new byte[1024];
>  int count = 0;
>  while(1024 == (count = in.read(buffer)))
>     out.write(buffer);
>  out.write(buffer, 0 count);
>  in.close();
>  out.close();
> }
> 
> Hope that helps,
> 
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
> 
> iD8DBQFF7fGd9CaO5/Lv0PARAs09AJ9O85lqIEw+ouuSo3oOJnvWQNkl+wCff4c8
> 8QYH36U+LBA0jk9GXbt69HQ=
> =Uz+L
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Servlet-Filtering---performance..-tf3356429.html#a9347741
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: [OT] Servlet Filtering & performance..

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

prt,

prt wrote:
> I have another problem,

You should post a separate message in this case.

> I save uploaded images(by users)  in folder place at "D:\storeg\data\uf\".
> So when the URL is "http://localhost/test/uf/xyz.jpg" then is redirect to
> "D:\storeg\data\uf\xyz.jpg" 

I assume you mean "it is mapped to d:\..."... redirecting to a local
file path won't work unless you are running your browser from the
application server.

> And that so because i configure in server.xml this part between the host
> tag,
> <Context path="/uf" docBase="D:\storeg\data\uf" debug="0"
> crossContext="false"/>
> 
> Is there way that i can do that by using the filter, how can i redirect the
> request from the filter when is "/uf..." to
> "D:\storeg\data\uf..." with out loading the image and print it out from the
> filter ?

You have two options:

1. Map a directory into your URL space (not sure how to do that in
   Tomcat, but I'm sure that's been covered in the archives).

2. Write an image-serving servlet. Something like this:

<servlet-mapping>
   <url-pattern>/ug/*</url-pattern>
   <servlet-name>imageServlet</servlet-name>
</servlet-mapping>

Your servlet class, written in loose pseudocode:

doGet()
{
 String filename = request.getExtraPathInfo();

 // You should check for ".." and other evil-looking paths

 filename = "D:/storeg/data/ug/" + filename;

 File f = new File(filename);

 response.setContentType("whatever/is+appropriate");
 response.setContentLength(f.length());

 FileInputStream in = new FileInputStream(filename);
 OutputStream out = response.getOutputStream();

 byte[] buffer = new byte[1024];
 int count = 0;
 while(1024 == (count = in.read(buffer)))
    out.write(buffer);
 out.write(buffer, 0 count);
 in.close();
 out.close();
}

Hope that helps,

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF7fGd9CaO5/Lv0PARAs09AJ9O85lqIEw+ouuSo3oOJnvWQNkl+wCff4c8
8QYH36U+LBA0jk9GXbt69HQ=
=Uz+L
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: [OT] Servlet Filtering & performance..

Posted by prt <sh...@pelephone.co.il>.
Well so you say is good, thank you for your help.

I have another problem,
I save uploaded images(by users)  in folder place at "D:\storeg\data\uf\".
So when the URL is "http://localhost/test/uf/xyz.jpg" then is redirect to
"D:\storeg\data\uf\xyz.jpg" 
And that so because i cunfigure in server.xml this part between the host
tag,
<Context path="/uf" docBase="D:\storeg\data\uf" debug="0"
crossContext="false"/>

Is ther way that i can do that by using the filter, how can i redirect the
request from the flter when is "/uf..." to
"D:\storeg\data\uf..." with out loading the image and print it out from the
filter ?

Thank you. (it take hourse to post amessage, my english is not good, sory)


Tim Funk wrote:
> 
> You won't even notice this code being run.
> 
> -Tim
> 
> prt wrote:
>> Hi,
>> My list is small only 5 URLS, 
>> I save it in (String[]), that load in the Filter init method by init
>> parameter in the web.xml.
>> Every request i take the ServletPath and to this part in function,
>> 
>> boolean bRunURL;
>> 
>> for(int i = 0, max = sArrAlowd.length; i < max && !bRunURL; i++)
>>     bRunURL = (sServletPath.indexOf(sArrAlowd[i],1) == 1);
>> 
>> return bRunURL;
>> 
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Servlet-Filtering---performance..-tf3356429.html#a9339522
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: [OT] Servlet Filtering & performance..

Posted by Tim Funk <fu...@joedog.org>.
You won't even notice this code being run.

-Tim

prt wrote:
> Hi,
> My list is small only 5 URLS, 
> I save it in (String[]), that load in the Filter init method by init
> parameter in the web.xml.
> Every request i take the ServletPath and to this part in function,
> 
> boolean bRunURL;
> 
> for(int i = 0, max = sArrAlowd.length; i < max && !bRunURL; i++)
>     bRunURL = (sServletPath.indexOf(sArrAlowd[i],1) == 1);
> 
> return bRunURL;
> 

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: [OT] Servlet Filtering & performance..

Posted by prt <sh...@pelephone.co.il>.
Hi,
My list is small only 5 URLS, 
I save it in (String[]), that load in the Filter init method by init
parameter in the web.xml.
Every request i take the ServletPath and to this part in function,

boolean bRunURL;

for(int i = 0, max = sArrAlowd.length; i < max && !bRunURL; i++)
    bRunURL = (sServletPath.indexOf(sArrAlowd[i],1) == 1);

return bRunURL;

U think is ok ?


Tim Funk wrote:
> 
> If your allowed list check is small and simple (and NOT synchronized) - 
> the performance impact is probably not noticeable.
> 
> If your list of URLS is large - then your check would be based on your 
> check algorithm. (http://en.wikipedia.org/wiki/Big_O_notation)
> 
> -Tim
> 
> prt wrote:
>> Hi to all again,
>> 
>> I added filter object for all requests  <url-pattern>/*</url-pattern>.
>> In the Filter class i check the ServletPath
>> (HttpServletRequest.getServletPath()) and
>> i check if is equals to alist of allowd paths. it is work just perfect.
>> But i think that is damage the performance, is it ?
>> 
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Servlet-Filtering---performance..-tf3356429.html#a9335687
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Servlet Filtering & performance..

Posted by Tim Funk <fu...@joedog.org>.
If your allowed list check is small and simple (and NOT synchronized) - 
the performance impact is probably not noticeable.

If your list of URLS is large - then your check would be based on your 
check algorithm. (http://en.wikipedia.org/wiki/Big_O_notation)

-Tim

prt wrote:
> Hi to all again,
> 
> I added filter object for all requests  <url-pattern>/*</url-pattern>.
> In the Filter class i check the ServletPath
> (HttpServletRequest.getServletPath()) and
> i check if is equals to alist of allowd paths. it is work just perfect.
> But i think that is damage the performance, is it ?
> 

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org