-1

I have two servlets (e.g. servlet1.java, servlet2.java). I want to run two servlets in parallel with one request. How can I achieve this?

I have some idea about multithreading concepts but I have no idea how to implement this.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
suresh s
  • 3
  • 5

3 Answers3

2

Actually there is no way to do that. For a single HTTP request you cannot pass it to two servlets.

The request belongs to only one servlet.

In a Java EE application every servlet acts as a thread.

One HTTP request belongs to one servlet only. Maybe an HTTP response can be passed to some other servlet (servlet chaining).

Arjan Tijms
  • 36,666
  • 12
  • 105
  • 134
Vignesh Shiv
  • 1,079
  • 7
  • 20
0

Really not sure why you wold want to do this? What's your use case?

How would the response form each servlet be handled and returned back to the client?

If you need to spawn a thread during the servlet's doGet or doPost method, then I suggest taking a look at this answer

Depending on your usecase though it may be better to implement your thread using a servlet filter.

Community
  • 1
  • 1
Steve Atkinson
  • 1,175
  • 2
  • 10
  • 26
0

If you want a single thread to pass through two classes on server side, think of using Servlet filters

Sid
  • 4,510
  • 14
  • 51
  • 100