Learn the servlet is really a little tired ah, study for so long or just get started. But now the study enthusiasm is still very high. Come on.
The servlet "1" blog has written about how to use the method of implementing the Servlet interface to write servlet program, and write in the Web.xml file for configuration, run successfully, then say how the implementation of the service. At the same time, this is also one of the face questions.
When the Servlet interface is implemented with four copies, you can describe the servlet's workflow (lifecycle) by stating the use of four methods:
If you look at these functions seriously, you will find that the servlet Java program does not have a main function, so this is the server that is helping us invoke these methods.
The servlet program is invoked by the Web server, and the Web server receives the client's servlet request;
(1) When the servlet is invoked for the first time, the init function is triggered, which loads an instance of the servlet into memory, and the INIT function is invoked only once.
(2) Then invoke the Servlet's service function
(3) Call the service function directly when accessing the servlet after the second time
(4) When the Web application reload, turn off Tomcat or shutdown, the Destroy function is invoked and the servlet is destroyed.
At the same time, the method of inheriting HttpServlet to develop the servlet (developed by this method in software company 90%), where the designer handles post submission and get commits separately in HttpServlet. Then you have to talk about the difference between the two submissions.
The difference between get and post commits:
1. From a security perspective Get<post,get submitted data is displayed in the address bar of the browser
2. From the size of the submitted content Get<post,get can not be greater than 2K, and post submission is theoretically unrestricted, but not greater than 64K.
3. From the request response speed, Get>post,get requires the server to process the request immediately, and the POST request may form a queue.
Here is a small program of objects, you can look at first.
public class Servlet1 extends HttpServlet {
int ticket=2;
public void doget (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
Response.getwriter (). println ("Linweieran," +new java.util.Date (). toString ());
public void DoPost (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
//general developers, the habit of Doget () DoPost ().
This.doget (request, response);
}
The work mechanism is first mentioned in this article, which is loaded into memory after the servlet has been accessed for the initial time, and then the instance is used for each request, which is a single example in use. When it comes to a single example, it can be thought that when multiple users access a variable, thread safety issues occur. So there's a principle when writing code.
(1) If a variable requires more than one user to share, then the synchronization mechanism should be added when accessing the variable.
Synchronized (object) {
Synchronizing code
}
(2) If a variable does not need to be shared, it is defined directly in Doget () or Dopost ().
The following is a ticket to the small program, in order to simulate the time delay of the visit to the ticket site specifically added sleep 10 seconds, in order to synchronize ticket use synchronized method.
public class Servlet1 extends HttpServlet {
int ticket=2;
public void doget (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
Response.getwriter (). println ("Lijingbo," +new java.util.Date (). toString ());
Simple workaround
Synchronized (this) {
if (ticket>0) {
System.out.println ("You buy tickets");
Hibernate
try {
thread.sleep (10*1000);
} catch (Interruptedexception e) {
//TODO auto-generated catch Block
e.printstacktrace ();
}
ticket--;
} else{
System.out.println ("You have got Tickets");}
public void DoPost (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
//general developers are accustomed to combining doget () DoPost ().
This.doget (request, response);
}
The results of the operation show that it is guaranteed that only two people will be able to buy tickets.