Servlet seems to handle multiple concurrent browser requests synchronously
Your browser is apparently using the same HTTP connection in different windows. The servlet container uses a single thread per HTTP connection, not per HTTP request. You should run two physically different webbrowsers to test this properly. E.g. one Firefox and one Chrome.
How does a single servlet handle multiple requests from client side
Struts/Spring frameworks are actually written on top of Servlet specification so doesn't matter what you use underneath it use Servlets.
You are right, Only single instance of Servlet is created, but that instance is shared across multiple threads. For this reason you should never have shared mutable states in your Servlets.
For example you have following servlet mapped to http://localhost/myservlet
class MySerlvet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) {
// Get Logic
}
}
The Web Server will have something similar (Not necessarily same) in its code.
MyServlet m = new MyServlet(); // This will be created once
// for each request for http://localhost/myservlet
executorService.submit(new RequestProcessingThread(m));
Preventing concurrent access to a method in servlet
One way you could solve the problem is by using a synchronized
block. There are many things you could choose as your locking object - for the moment this
should be fine:
class BookingService {
public void insert(Booking t) {
synchronized(this) {
if(available(t.getTutor(), t.getDate(), t.getTime())) {
bookingDao.insert(t);
} else {
// reject
}
}
}
}
If you have more than one instance of the servlet, then you should use a static object as a lock.
Related Topics
How to Disable or Bypass Hardware Graphics Acceleration(Prism) in Javafx
Dependency Injection and Javafx
How to Read Input Character-By-Character in Java
Log4J: How to Use Socketappender
Simple Way to Count Character Occurrences in a String
Singleton Design Pattern: Pitfalls
How to Keep the Iteration Order of a List When Using Collections.Tomap() on a Stream
Is There a Difference in Removing the Curly Braces from If Statements in Java
JSONmanagedreference VS JSONbackreference
Java Regex: Repeating Capturing Groups
How to Test That No Exception Is Thrown
How to Use Java to Read from a File That Is Actively Being Written To
How to Change Timezone for a Java.Util.Calendar/Date
Define a Fixed-Size List in Java
Sorting a List with Stream.Sorted() in Java
Validation of a List of Objects in Spring
Convert a Date Format in Epoch
Tomcat 8 Is Not Able to Handle Get Request with '|' in Query Parameters