1

I have a doubt about how a servlet is instantiated. Actually i have reasons to think that it is instatiated only once when the app loads or when it receives the first request. And not one instance for each request it receives. Could anyone clear this doubt?

The reason to think like that is that the servlet class has a "String var = null" member and it seems to have a null value only the first time it receives a reques.

Thank you very much

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
mdev
  • 472
  • 7
  • 18
  • They're more or less instantiated once, but they *may* be loaded by the container, for various reasons, again. Not per-request, and this may have changed in more recent servlet specs. – Dave Newton Nov 22 '13 at 23:06
  • possible duplicate of [Difference between each instance of servlet and each thread of servlet in servlets?](http://stackoverflow.com/questions/2183974/difference-between-each-instance-of-servlet-and-each-thread-of-servlet-in-servle) – Kevin Panko Nov 23 '13 at 05:38

2 Answers2

3

Only one instance of the servlet is created. This is why it is so important not to include fields in your servlet that are used within the get and post methods. A class instance variable (field) within a servlet is not thread safe and could be modified by multiple requests resulting in unexpected behavior.

Consider the following example:

ServletTest.java

@WebServlet("/ServletTest")
public class ServletTest extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private Integer increment = 0;

    public ServletTest() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(increment++);
    }

}

test.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>  
<form action="ServletTest" method="post">
    <button type="submit">Submit</button>
</form>
</body>
</html>

If you run this example and hit the submit button it will print 0 to the console. Subsequent submits of the form will print 1, then 2, etc... This proves the servlet is the same instance.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

It's been a while since I've played with servlets. But here is an experimental approach. Why don't you create a method in your servlet (say override the doGet).

In this method do this: System.out.println(this.hashCode()); or even better System.out.println(this);

Then make GET requests to it and observe what's printed out.

How do servlets work? Instantiation, sessions, shared variables and multithreading

This also seems useful.

Community
  • 1
  • 1
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • Yea. It seems so far that we all agree that servlets are instantiated only once or at least we must assume that when we write them. My doubt was just because I was used fields in my servlets. Was wondering if it could be a very bad practice. Thank you very much for your answer. – mdev Nov 23 '13 at 00:03