Servlet Threads

i have a servlet written. i wanted to make it multi-threaded so that if the request is taking time to be processed then another request will not have to wait.
so when the request comes in, i start a new thread and pass the HttpServletResponse to that thread. Then the servlet is free to accept other requests.
the problem i am having is that when the browser makes the request, i can see on the server through debug that the correct information is being built but nothing is being written back on the outputstream?
am i threading the servlet wrong? or should i be using a different means?
thanks,
hande.

Servlets are already multi-threaded and should be able to handle multiple requests.

Similar Messages

  • Multiple Servlet Threads - per HttpSession

              Can there be concurrent Servlet threads on a single HttpSession in cases where
              there are multiple frames on the Browser Window, or if the user has multiple Windows
              open in the same Browser session? Or is it the case that the multiple requests
              on that HttpSession are synchronized to be done in sequence (WebLogic 6.1 as Web
              Server).
              If there can be concurrent threads then how are non-repeatable reads handled for
              objects stored in the HttpSession. For example two threads pick up object A from
              HttpSession, and overwrite each other changes when they set them back in HttpSession.
              Thanks,
              Pradeep
              

              Thanks but how does that protect against non-repeatable reads?
              Even if there is a synchronized session object in HttpSession still nothing to
              stop from concurrent threads from overwriting each other's settings if the threads
              picked up a value from session object, did some processing and wrote it back.
              (This is almost an optimistic concurrency type scenario)?
              Pradeep
              Alexander Petrushko <[email protected]> wrote:
              >Pradeep Behl wrote:
              >
              >> Can there be concurrent Servlet threads on a single HttpSession in
              >cases where
              >> there are multiple frames on the Browser Window, or if the user has
              >multiple Windows
              >> open in the same Browser session? Or is it the case that the multiple
              >requests
              >> on that HttpSession are synchronized to be done in sequence (WebLogic
              >6.1 as Web
              >> Server).
              >>
              >> If there can be concurrent threads then how are non-repeatable reads
              >handled for
              >> objects stored in the HttpSession. For example two threads pick up
              >object A from
              >> HttpSession, and overwrite each other changes when they set them back
              >in HttpSession.
              >
              >It's up to the application to prevent the threads from stepping on each
              >other when it
              >comes to HttpSession. You can create a thread-safe session class, store
              >your session
              >objects in the instance of that and then have HttpSession store the thread-safe
              >impl.
              >
              >Cheers,
              >
              >Alex
              >
              >
              

  • Servlet thread pool

    Is there any way I can create my own init() for the threads in the servlet thread pool (by overriding some method or through other means)? (Not to be confused with the servlet's init().)

    web server itself is a thread pool implementation, unlesss your thread has other purpose, if not it is suited and enough for you do not implement another thread for it. If you want some init method, you can call from other class before start of your thread pooling.
    Hopefully can help you...

  • Servlet Thread Pools for iPlanet WS 4.1

    We have iPlanet WS 4.1 SP 5, running on HP UX. Wanted to make sure servlets run fast, so set up servlet thread pool. Initial result was that even the startup servlet could not be loaded, got stack overflow. Reduced number of threads from 60 to 5. Now startup servlet kicked off OK, but a subsequent servlet processing a request bombed with same error. This is obviously some kind of resource issue; how does one allocate enough memory or whatever it needs to handle the thread pool? Do I really even need a thread pool?

    Thanks, and a follow-up question: at that reference, it seemed to imply there is really no need to allocate a user thread pool if running on Unix. Could even slow down the application. Am I likely to be OK with about 300 concurrent users if I don't allocate a thread pool? I was afraid they'd be single-threaded through the front controller servlet if I did not specifically allocate and use a thread pool, but perhaps multiple threads are already built into the container?

  • Are servlets thread safe??

    Are servlets thread safe?? if not, then how to make them thread safe.

    Hi,
    servlets aren't thread-safe; you can make them thread-safe by implementing the interface javax.servlet.SingleThreadModel
    From the javadoc:
    public interface SingleThreadModel
    Ensures that servlets handle only one request at a time. This interface has no methods.
    If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method. The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet.
    If a servlet implements this interface, the servlet will be thread safe. However, this interface does not prevent synchronization problems that result from servlets accessing shared resources such as static class variables or classes outside the scope of the servlet.

  • Problem regarding servlets+ threads

    <meta http-equiv=\"refresh\"" " content=\"5\" ";URL=\"\">";
    Iam using meta refresh tag for refreshing browser for every 5 seconds.
    When i invoke doPost () then iam creating thread and for every 5 sec it calls doGet() .
    If i close browser then servlet is not invkoing but thread is running
    How to stop a thread when we close browser?

    iam using Tomcat
    <Connector acceptCount="100"
    connectionTimeout="20000"
    disableUploadTimeout="true" port="8080"
    redirectPort="8443" maxSpareThreads="75"
    maxThreads="150" minSpareThreads="25">
    </Connector>
    his is ok.Ignore my first post.
    Your problem has nothing to do with the containers thread management.
    Is there any way to excute following method
    Thread.currentThread().destroy(); or stop when i
    close browserPlease see malcolmmc's great post (i.e, the browser refresh is driving things - you shouldn't need a thread in the servlet).
    Servlets are blocking request / response based: When you've written a response, you're done. They are not connection based - and there isn't (and shouldn't be) any direct way to know when a connection has gone

  • Servlet Threading

    I have written a servet that takes in all the request
    parameters from a Front-End HTML Form.
    I want to make this servlet Multi-Threaded so that concurrent users can enter data in the Form and this should be handled by the servlet.
    Can any one please advise how to make this servlet multi threaded.?
    For the moment,I have defined all the request variables in the doGet() method,as each instance of doGet() will run in a separate thread.

    Your servlet is already multithreaded. You may run into problems depending on what you are going to do with the HTML Form's contents. If you are saving the contents to a text file or as an applciation context variable, for example, you will need to provide synchronization. If you are accessing a DB, synchronization is probably provided for you.
    If all you are doing is echoing back the contents of the HTML for or returning a computation, then you are probably OK.

  • Xsql servlet threading model?

    does anybody know the threading model supporting by the xsql servlet? from my testing, it appears that it only supports single threaded.
    environment: nt 4.0, svcpack 5/iis/servlet exec 2.2/jdk 1.1.8/xsql servlet 0.9.6
    any help/info appreciated.
    thanks
    michael lasmanis

    steve,
    here are the java vm settings from servlet exec. all the other settings are in line with the install instructions.
    VM Settings
    Java VM: 1.1.8 from Sun Microsystems Inc.
    JITC: symcjit
    Host: Windows NT 4.0 on x86
    Native Stack Size: 128K Bytes
    Java Stack Size: 400K Bytes
    Minimum Heap Size: 1024K Bytes
    Maximum Heap Size: 524288K Bytes
    Verbose GC: Disabled
    Verbose: Disabled
    JITC: Enabled
    Class GC: Enabled
    Async GC: Enabled
    Verify: Remote
    Java VM: Sun Classic
    null

  • Is Servlet thread safe?

    Hi,
    I have created a application. All that the application does is that it listens at a predefined port say 8080 and for every request that is made to the application, the request header information is dumped into a file.
    I have implemented it by creating a servlet that implements the Filter interface and implemented its doFilter() method.
    In the method, I simply make use of the Servlet Request object and wite the header details in a file.
    The question : Is the above written Servlet class - thread safe?
    There are no class member variables defined.
    I have deployed the application on Tomcat 5.0.19.
    Regards,
    Montek Singh Ahluwalia

    Yes, Servlets are thread safe.
    Although it is standard to have one servlet instance per registered servlet name, it is possible for a servlet to elect instead to have a pool of instances created for each of its names, all sharing the duty of handling requests. Such servlets indicate this desire by implementing the javax.servlet.SingleThreadModel interface. This is an empty, tag interface that defines no methods or variables and serves only to flag the servlet as wanting the alternate life cycle.

  • Servlet, threads and StaleConnectionException

    Hi folks,
    we're currently developing an J2EE application in which we have to works with threads started and stopped via web interface: on a test JSP page, we have a button to start the thread and another button to stop the same thread. At the moment, we have just one thread which periodically checks some data on a DB installed on the same machine on which the application server runs.
    The problem is, when the thread is restarted, a StaleConnectionException is thrown, which code SQL1224N The DB is DB2 from IBM and the application server is WebSphere 5; both of they run on a Linux Red Hat system.
    We use a JDBC Connection provided by the application server.
    We can't understand where is the problem. We suppose that the problem is creating independent threads from a servlet, as J2EE paradigm suggests not to do so. Is there anybody that encountered the same (or similar) problem ? Any suggestion ?
    Thanx a lot in advance.
    Claudio.

    Hi Claudio,
    hope this helps ...
    http://archive.midrange.com/java400-l/200301/msg00039.html
    WS App Throwing a StaleConnectionException
    Q: My WebSphere application is throwing a
    com.ibm.websphere.ce.cm.StaleConnectionException. What is the problem?
    This exception indicates that the connection is no longer valid. Here are some
    possible causes of it:
    1)The database management system is down.
    2)The application previously closed the connection.
    3)The connection has been orphaned, which means that the application has held
    the connection for too long without using it. The Data Source property "Orphan
    timeout" specifies the maximum number of seconds that an application can hold a
    connection without using it. If an app holds a connection without using it
    beyond the Orphan timeout value, WebSphere may return the connection to the
    connection pool. If the connection is returned to the connection pool, and the
    application then tries to use the connection, the Stale ConnectionException is
    thrown.

  • Servlet thread programming

    Hello,
    I have a non-blocking IO channel server that gets instantiated through a thread. The server has to listen for a pre-specified number of clinet connections and close down. This is repeated a few times. The servlet calls two separate threads. One of the two threads calls the server.
    The server works just fine till the time it is called from a tomcat servlet. On being called from a servlet the first time, the server starts listening, formats and displays the output correctly. However, the socket does not stop listening on the port[I have specifically put socket.close() in there]. As a result the next socket that gets started, simply dies due a bind error.
    The code works perfectly outside the web-container (tomcat) environment.
    Also, the ethereal output for both connections is identical; no extra FIN-ACKs or ACKs flying through.
    Thanks in anticipation.

    I tried increasing the value of the loop as suggested in the earlier mail, but that just kept the server hanging even after all communications.
    Just noticed something funky. The servlet page sends the HTTP page to the requesting web-client immediately after the Fin ACK from the TCP client. The server then ACks the FIN ACK but never sends a FIN ACK back to the client. This causes the server to stay up. If the HTTP page is not served out immediately, the communication ends properly.
    Any clue on how to do this?
    Thanks

  • Servlets, Threads, sleep and wait

    Hi,
    I am new to servlet programming and have a problem.
    I want to create a servlet that is also Runnable. It should hold the HTTP request open, and then wait() or sleep(), waking up regularly to send data to the client.
    My code seems to work OK, and in the server logs I can see the debug messages where my servlet is sleeping and waking up. However, it doesn't keep the connection open. Instead, any clients that try to connect instantly receive an empty response.
    Can anyone explain why this is? I am a bit lost. Is the server (Tomcat) cutting my request off when I start to sleep or what?
    dave

    OK. Now seems like I have it working with sleep().
    I was doing something pretty dumb and assuming that a new HttpServlet instance was created for every request - doing things like this.request = request; - obviously this didn't work too wlell ;-)
    Now just using Thread.sleep() it works much better
    One more question - how can I get a separate servlet to call notify() on this servlet so that it wakes up and delivers its message? (I know I need to call wait() not sleep())
    Thanks
    dave

  • Servlets, Threads and ThreadNames and Tomcat!

    Hi gurus's
    I found something interesting but am not sure if this is in the servlet container spec.....
    When a new client request comes in, servlet serves that request on a separate thread. Now I change the thread name for some requests. I found that servlet container (Tomcat 4.1.24 in my case) seems to be reusing the threads to serve client requests. i.e when another request comes in (after the first request has finished) the thread name is still set to what I had set it to during the first request....
    This implies that Tomcat actualyl re-uses the threads it creates to serve client requests. I thought that it would create a completely new thread for each request.
    Does anyone know anything about this??? Or is this in the spec for container writers where they start n number of threads and just pool these threads and re-use them to serve client requests???
    I would appreciate your help because this causes problems with my logging mechanism as I rely on unique thread name...

    The servlet specs does not require servlet containers to start a new thread for each request.
    For performance purposes, most, if not all, servlet containers re-use threads to service requests.
    You may want to switch over to the Log4J (http://jakarta.apache.org/log4j) logging mechanism. Its concept of Nested Diagnostic Contexts (NDCs) is much better than relying on the uniqueness of thread names.

  • Servlet thread safety

    I am new to Java and servlets. I am experimenting with servlets. I have separate process that puts files on my server periodically. I have written a servlet that reads the folder and determines there is a batch of files that need to be uploaded (ftp'd) to another server. I use apache commons ftp to send the files. The batch size varies, sometimes its 1 file, sometimes as many as 5 files. I find that infrequently, the ftp doesn't complete. I have started using log4j and what I find is that my servlet with ftp will send files 1 and 2 correctly, but after the ftp login and starting the ftp on file 3 of 5, my logging suddenly stops and then I start getting logging on file 4. Its as though something has forced it to jump
    I am not seeing any exceptions. I log each part of the ftp process, the connection, the login, changing folders, sending the file, etc. I'm not sure yet if it is always in the same spot in the list of files.
    I'm concerned there may be some concurancy problem. Its almost like I have my web service starts to run and then part way through, another request for the web service interrupts it and takes control. I am using Tomcat.
    Any suggestions? How do I track it.

    Just keep in mind that only one instance of a servlet will be created during application's lifetime and that this instance is been reused among all requests.
    So if you think logically, anything which you assign as servlet instance variable is NOT threadsafe and anything which you assign as method local variable IS threadsafe. That's all.

  • Thread management in servlet

    Here is the problem that i am trying to solve - and get better understanding of issues to look out for.
    1) Problem to solve
    Clients sends an http request to an distributor server. The request contains a list of tasks. Each such task can be sent to a particular handling server (out of a known set) which can handle the request. Each task has a task type, which identifies which server the request needs to be passed to. The responses from the handler servers are then to be collected and passed back the client as a consolidated response.
    On the distributor server, the servlet thread that is handling the request will
    a. need to spread the requests for various handling servers
    b. send the requests in parallel to handling servers
    b. get all the responses , consolidate and send back
    2)Proposed solution
    I was thinking of having a thread pool - one thread for each handling server, initialized within servlet . When the distributor server receives a request - the tasks would be spread to these handling threads. The distributor thread would then wait, until the handling threads notify.
    When a notification is received - the distributor thread will need to check if all responses are received. If not - it would need to wait (till a max_timeout) else return the response to client
    3)Questions
    - This involves waiting on the thread created by serlvet container (the distributor thread) and creating a threadpool. Thread creation and management by application in web-container is generally dissuaded - but given this issue - i see no other alternative
    suggestions ? Its been a long email - thanks for considering
    Fred

    How many clients for the entry server do you presume for the application ? I ask this because it is very possible that the server to remain without available threads and give errors. If you use different servers for the final requests I suppose that are some very time consuming processes. What kind of application are running on these servers so that you want to use this kind of distributed computing ?
    Why did you choose as an entry point a servlet container and http request for this kind of problem ? Do you need a graphic response in the browser ? If the response time is too long, the browser (Internet Explorer) will show exception and will not wait for the response (for this you must set some variabiles on client). Also the J2EE server must be set so that not to throw a time-out exception.
    To prevent these, you could do so that not to block the distributor thread and let it send the answer back to the client. Then you can tell to client browser to query the server from time to time to check the status of the execution (you can do that from javascript). When the execution is ready you can retrun the response to the client. Doing this way you will not block the application server threads.
    Now in the back end, you must somehow to take some identificators from the processes and sent them back to the client.
    You can use a database to make the bridge between the central server and the other processes (here you can put the processes identificators). When the processes end up they update their results in the database.
    The client, when will make the repetitive requests, will check the status of the processes in the database with given identificators.
    Sounds like an interesting project.

Maybe you are looking for

  • "Show in Mail" link in To-Dos - can't get rid of, like a virus

    Hello! I use To-Dos in iCal intensively. But strange thing started to happen - all my To-Dos magically get "Show in Mail" link in URL field. When I enter a new To-Do - everything is fine, but after a couple of days "Show in Mail" link sticks to it ag

  • Garage Band Loops are missing.

    I went to the Apple store to have iLife 09 installed onto my computer, but when I opened up Garage Band, almost all of the loops were missing. How do I go about getting them back?

  • Cable trouble

    my friend has recently bought a new iPod mini and the cable doesn't work. if you connect it to the iPod and the computer it wont connect, but if i use my cable it works. can she get a replacement under warranty?

  • Finally! Here is solution to Error 46 for Vista Users

    run regedit go to HKCR\QTOLibrary.QTControl right-click, Permissions add permissions for group "users" set to "Full Control" repeat for HKCR\QTOLibrary.QTControl.1 (You may need to apply same changes to these keys as well) HKCR\QuickTime.QuickTime HK

  • New demos for Forms9i, how to install them ?

    How do i have to install the demos which comes with demos9_0_0_0_8.zip file ? Thanks Hans