Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Tomcat and filter servlet

Reply
Thread Tools

Tomcat and filter servlet

 
 
eunever32@yahoo.co.uk
Guest
Posts: n/a
 
      09-28-2005
I would think you have a pool of Tomcat threads
each of which will get stuck (depending on the type of requests being
submitted)

Eventually when all threads get stuck the server will be unavailable

 
Reply With Quote
 
 
 
 
Bryce
Guest
Posts: n/a
 
      09-28-2005
On 27 Sep 2005 10:48:58 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>>But the OP was talking about Filters not Servlets (even though he
>>calls them Filter Servlets)

>Yes, I am talking about filters and i guess they are also servlets.
>If the filter doesnt implement singlethreadmodel , then what will
>happen?
>Will multiple requests go through different copies of filter object?
>What does actually happen? Some explanation with physical
>interpretation (like if multiple copies of filter object are made in
>memory ) would be helpful.


Wrong assumption. Filters are not Servlets. A Filter implements the
interface javax.servlet.Filter. There's no way to specify that its a
single threaded model, or otherwise.

If you get into an infinite loop, that request will never make it to
the Servlet (or back to the requestor if you are stuck after the
doFilter()).

--
now with more cowbell
 
Reply With Quote
 
 
 
 
pvsnmp@yahoo.com
Guest
Posts: n/a
 
      09-28-2005
>Wrong assumption. Filters are not Servlets. A Filter implements the
>interface javax.servlet.Filter. There's no way to specify that its a
>single threaded model, or otherwise.
>If you get into an infinite loop, that request will never make it to
>the Servlet (or back to the requestor if you are stuck after the
>doFilter()).


Hi ,
thanks for correcting me,
But can you please tell me what happens with the subsequent requests
after the filter gets into infinite loop?

thanks and rgds,
Prashant

 
Reply With Quote
 
pvsnmp@yahoo.com
Guest
Posts: n/a
 
      09-29-2005
Hi,
This is what I am trying to do . Can someone tell me if i will ever see
the counter increase to more than 1. There are no other filters. The
chain.doFilter(req,res) will send the request to the destined servlet.
.................................................. .....................................
import java.lang.*;
import java.net.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;

public class Chameleon implements Filter{
static ThreaCounter tc;

public void init(FilterConfig conf) throws ServletException{
tc=new ThreadCounter();
}

public void doFilter(ServletRequest req, ServletResponse
res,FilterChain chain) throws IOException, ServletException{
tc.thrd_Incre(true);
chain.doFilter(req,res);
tc.thrd_Incre(false);
}

public void destroy(){
}
}


class ThreadCounter{
int thrd_num;

public ThreadCounter(){
thrd_num=0;
}

synchronized void thrd_Incre(boolean add){
if(add){
thrd_num++;
}
else{
thrd_num--;
}
}
}
.................................................. ................................

rgds,
Prashant

 
Reply With Quote
 
Bryce
Guest
Posts: n/a
 
      09-29-2005
On 28 Sep 2005 13:59:40 -0700, (E-Mail Removed) wrote:

>>Wrong assumption. Filters are not Servlets. A Filter implements the
>>interface javax.servlet.Filter. There's no way to specify that its a
>>single threaded model, or otherwise.
>>If you get into an infinite loop, that request will never make it to
>>the Servlet (or back to the requestor if you are stuck after the
>>doFilter()).

>
>Hi ,
>thanks for correcting me,
>But can you please tell me what happens with the subsequent requests
>after the filter gets into infinite loop?


I've never used the Single THreaded Model, so I don't konw about that.
In a Normal Servlet, each request is handled by its own thread.
Subsequent requests will get through..

Note: There's a finite number of threads, so if it happens on every
request, eventually, it will make your app non-responsive.

--
now with more cowbell
 
Reply With Quote
 
pvsnmp@yahoo.com
Guest
Posts: n/a
 
      09-29-2005
>I've never used the Single THreaded Model, so I don't konw about that.
>In a Normal Servlet, each request is handled by its own thread.
>Subsequent requests will get through..


Hi,
Does your explanation above apply to the case when there is a filter
before the servlet?I am not using SingleThreadedModel either.

rgds,
Prashant

 
Reply With Quote
 
Bryce
Guest
Posts: n/a
 
      09-30-2005
On 29 Sep 2005 06:10:18 -0700, (E-Mail Removed) wrote:

>>I've never used the Single THreaded Model, so I don't konw about that.
>>In a Normal Servlet, each request is handled by its own thread.
>>Subsequent requests will get through..

>
>Hi,
>Does your explanation above apply to the case when there is a filter
>before the servlet?I am not using SingleThreadedModel either.


Yes.

A filter wraps around a servlet. Take the following Filter:

public class MyFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse
response, FilterChain chain) {

System.out.println("Before Servlet");

chain.doChain(request, response);

System.out.println("After Servlet");
}
}

The first part is executed before the servlet is run, and the last
part after.

Do this experiment.

1. Load the FIlter and Servlet in your favorite IDE.
2. Run Tomcat in Debug mode.
3. Put a break point somewhere in your Servlet.
4. Take a look at the stack trace.

You should see your Filter down in the stack trace... What does this
tell you?

With a Filter you can:
Do processing prior to executing the Servlet
Do post processing on the response after executing the servlet.

Hope this helps.

--
now with more cowbell
 
Reply With Quote
 
pvsnmp@yahoo.com
Guest
Posts: n/a
 
      09-30-2005
Hi,
Thanks,

rgds,
Prashant

 
Reply With Quote
 
Raymond DeCampo
Guest
Posts: n/a
 
      10-03-2005
Juha Laiho wrote:
> (E-Mail Removed) said:
>
>>>But the OP was talking about Filters not Servlets (even though he
>>>calls them Filter Servlets)

>>
>>Yes, I am talking about filters and i guess they are also servlets.
>>If the filter doesnt implement singlethreadmodel , then what will
>>happen?
>>Will multiple requests go through different copies of filter object?
>>What does actually happen? Some explanation with physical
>>interpretation (like if multiple copies of filter object are made in
>>memory ) would be helpful.

>
>
> Ouch; didn't check the specs last time to check whether filters
> are anyhow related to servlets, and actually I am too tired to do it
> now, either.
>
> Here's how it goes with servlets -- I don't know whether the
> SingleThreadModel has any effect on filters.
>
> - if the servlet does not implement SingleThreadModel, the servlet
> engine most possibly creates just one copy of the servlet object
> (the object will be created at latest at the point where it is
> called for the first time during the lifetime of the application
> instance; the engine is also free to let any servlet instance be
> garbage collected whenever there are no active requests being
> processed within the servnet instance)
> - corollary: multiple threads of control may be simultaneously
> executing within a single servlet instance - thus, servlets
> must be coded to be thread-safe
> - if the servlet does implement SingleThreadModel, then the servlet
> engine will make sure that just one thread of control at a time is
> executing within the service methods of the servlet -- however, the
> engine is completely free to create multiple instances of the
> servlet to provide for multiple simultaneous requests for the
> same servlet; as above, the servlet engine is also free to let
> the servlet instances be garbage collected
> - here the container handles the thread-safety, to some extent at
> least; I think there are some accesses (f.ex. to application
> context objects) where thread-safety must be explicitly coded for
>
> ... and writing this made me realize an interesting issue with the
> destroy() method of servlet. It's commonly known that one cannot
> trust that the destroy() is ever called. Now I also am aware that
> when the destroy() is called, other instances of the same servlet
> class may still be active. While this shouldn't be a concern in
> most situations, I think this was a worthwhile discovery..



The specification guarantees (in the non-SingelThreadModel) that only
one instance of the servlet is instantiated (per configuration in web.xml).

So you do not need to worry about multiple destroy() calls.

HTH,
Ray

--
XML is the programmer's duct tape.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javax.servlet and javax.servlet.http Andrew Thompson Java 1 04-25-2007 04:10 PM
Tomcat ssl servlet to servlet, private CA edavid3001@gmail.com Java 0 10-06-2005 09:03 PM
seeking servlet "Master" keep getting "Master/servlet/Master" not found. Tomcat 5.0.25 Doug McCann Java 1 08-05-2004 09:16 PM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
Tomcat: POST from one servlet or internal class to another servlet Sean Clarke Java 1 01-07-2004 02:22 PM



Advertisments