Servlet and JSP potential pitfalls
July 23rd, 2010 | Uncategorized | No Comments »
1.Servlet life cycle
Servlet life cycle is responsible for the Web container, when the first request Servlet client, the container is responsible for initializing Servlet, which is an instance of the Servlet class. After this instance of the client's request on the charge of the general will not instantiate other Servlet class, which is more than one thread using this instance. Efficient than the CGI Servlet reason is because the Servlet is multithreaded. If the Servlet is declared to be single-threaded model, then the container will maintain an instance of the pool, then the existence of multiple instances.
2.Servlet and thread safe JSP
Servlet specification has stated Servlet is not thread safe, so the time to note the development Servlet to this problem. Here 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;
/ / Servlet class simulation of multi-threaded situation
publicclassSmulateMultiThreadimplementsRunnable (
publicSmulateMultiThread () (
)
publicstaticvoidmain (String [] args) (
/ / Handle 100 requests
for (inti = 0; i <100; i + +)
(
newThread (newSmulateMultiThread ()). start ();
)
)
publicvoidrun () (
HttpServletRequestrequest = null;
HttpServletResponseresponse = null;
try (
WebContainer.getServlet (). doGet (request, response);
) Catch (IOExceptionex) (
)
catch (ServletExceptionex) (
)
)
)
/ / This is a Servlet class
classUnsafeServletextendsHttpServlet (
privateStringunsafe;
publicvoidinit () throwsServletException (
)
/ / ProcesstheHTTPGetrequest
publicvoiddoGet (HttpServletRequestrequest, HttpServletResponseresponse)
throwsServletException, IOException (
unsafe = Thread.currentThread (). getName ();
System.out.println (unsafe);
)
)
/ / This is the container class
classWebContainer (
privatestaticUnsafeServletus = newUnsafeServlet ();
publicstaticUnsafeServletgetServlet () (
returnus;
)
)
Output of the 100 different thread name, if 鏈?100 urge the deal by this Servlet, then there might be 100 species of unsafe to Zhi, last Kehu Duanqiangdedao the value of the error. Such as client a request thread called thread-1, but he may be returned to the thread-20. Reflected in the reality is, I logged in user name is user1, after landing into user2. So how is the Servlet Security then, any number of threads can be shared, do not use (instance variables + class variables), it's that simple. Can also be synchronized using the synchronization method, but this is not efficient, you can also use single-threaded model, so the efficiency is even lower, 100 requests came at the same time we must instantiate an instance of 100.
Method does not affect the temporary variable is thread-safe because they are allocated space in the stack, and each thread has its own private stack space.
3.JSP in thread-safe
JSP is the essence of Servlet, just to understand all the safety Servlet, Servlet and JSP security issues should be very easy to understand. Use of Statement of variables Servlet instance variables, not thread safe, others are thread safe.
/ / Not thread safe / / thread safe
Summary: thread-safety problem is caused by the instance variables, whether in the Servlet and JSP, Struts in Action or the inside, do not use instance variables, methods which are not there any instance variables, your program is thread-safe.
相关链接:
Bliss CDA Music APE to M4P Editor
DELL'S expansion overseas territory shake the HP printer status of the King
Multimedia effect is good, but not indiscriminate
LasVegas PS2 Converter
Youtube to Flash Guide
Articles ABOUT Science Education
What Is .mkv File
Career Planning: Attitude is Everything
Digital CDA Audio WAV to MP2 Ripping
Hot Terminal And Telnet Clients
ts format
YouTube Expected Profit Model
convert m4a To mp3
convert m4a TO m4r








