[英]init method is calling again and again in servlet
在servlet中的每个请求上都会一次又一次调用init
方法。 这是代码:
public class PersonInfoController extends HttpServlet {
private static final long serialVersionUID = 1L;
public PersonInfoController() {
super();
}
public void init() throws ServletException {
Connection connection = Database.getConnection();
System.out.println("init method");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<PersonInfoServiceI> myList = new ArrayList();
PersonInfoServiceI instance = new PersonInfoServiceImpl();
myList = instance.getdata();
String jsonstring = new Gson().toJson(myList);
request.setAttribute("List", jsonstring);
RequestDispatcher rd = request.getRequestDispatcher("showdata.jsp");
rd.forward(request, response);
}
public void destroy() {
System.out.println("the destory");
}
}
根据您的代码,当servlet将在第一个请求上加载时,init()仅应调用一次。 然后,销毁后,init()将在新请求时再次调用。 在此之间,只会调用您的服务方法。 您的代码很好,没有逻辑错误。 您在Servlet之外调用init方法吗? 您可以附加部署描述符吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.