servlets - only one stateless bean created -
i created 1 session bean , invoke method in servlet this:
@webservlet(name = "newservlet1", urlpatterns = {"/newservlet1"}) public class newservlet1 extends httpservlet { @ejb newsessionbean bean; protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); try { out.println("<!doctype html>"); out.println("<html>"); out.println("<head>"); out.println("<title>servlet newservlet1</title>"); out.println("</head>"); out.println("<body>"); out.println("<h2>"+bean.helloworld()+"</h2>"); out.println("<h1>servlet newservlet1 @ " + request.getcontextpath() + "</h1>"); out.println("</body>"); out.println("</html>"); } { out.close(); } }
and session bean:
@stateless public class newsessionbean { private int counter = 0; public newsessionbean() { system.out.println("constructor works"); } public string helloworld(){ return "hello are: "+counter++; }
when deploy sources see in glassfish 1 instance of session bean created - 'constructor works' once appeared. when try refresh site several computers see 1 instance of bean exists. when create 2 servlets use newsessionbean there still 1 instance. when read session beans understood there should pool of beans should several beans. why there 1 bean created in example. me understand situation.
there 1 servlet instance being created , since generates response quickly, not require additional stateless beans. noted, if implement helloworld
long operation, , generate new request before operation completes, you'll see new beans being created.
Comments
Post a Comment