What are the potential dangers of Servlet and JSP?
Editor to share with you what the potential dangers of Servlet and JSP are, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Life cycle of 1.Servlet
The Web container is responsible for the life cycle of Servlet. When the client requests Servlet for * times, the container is responsible for initializing the Servlet, that is, instantiating the Servlet class. In the future, this instance will be responsible for the client's request, and generally will not instantiate other Servlet classes, that is, multiple threads are using this instance. The reason why Servlet is more efficient than CGI is that Servlet is multithreaded. If the Servlet is declared as a single-threaded model, the container will maintain an instance pool, and there will be multiple instances.
Thread Safety of 2.Servlet and JSP
The Servlet specification has stated that Servlet is not thread-safe, so keep this issue in mind when developing Servlet. Here is a realistic model to illustrate the problem, first define a Servlet class, and then define a SmulateMultiThread class and WebContainer class.
Importjavax.servlet.http.HttpServlet
Importjavax.servlet.ServletException
Importjavax.servlet.http.HttpServletRequest
Importjavax.servlet.http.HttpServletResponse
Importjava.io.IOException
/ / this class simulates multithreaded Servlet
PublicclassSmulateMultiThreadimplementsRunnable {
PublicSmulateMultiThread () {
}
Publicstaticvoidmain (String [] args) {
/ / process 100 requests
For (inti=0;i