To introduce the servlet has a lot of text, I believe some friends like me hate to read a lot of text, I long story short:
As we have concluded, we use JavaBean programming to separate the HTML code of the Java code from the JSP file, but we cannot use the JSP built-in object in Java code, that is, JavaBean cannot use the request, Response and other objects to receive and transmit page information, only to achieve simple static functions.
A servlet is a dynamic reference to request, response ... Java class that runs on the server side like JavaBean. We call this the middle control layer (the controls) this is the Classic programming mode MVC programming pattern, the specific thing I will share with you in the next blog.
How the servlet is used I will not elaborate, a simple example: to find the area of the circle. The RADIUS information of the circle is sent from the client, which is then received by the servlet and returned to the client after the area is calculated.
First create a Srevlet class, write the Doget and Dopost functions, the other parts are automatically generated, do not need to write their own.
Second, we need to modify the Web site configuration file, XML, to add the following content: (Most of the eclipse is now automatically generated servlet configuration information, or can see)
For a detailed explanation of the comments in the code, finally we build a JSP to invoke the servlet:
OK, let's test it out:
Summary: Now we can see that the client interface is still only very short code, the middle of the process is running on the server side.
JavaBean can only handle static functions of the Java class and cannot get the parameters of the client. A servlet is a Java class that can fetch parameters from a client. What JavaBean and servlets have in common are performed on the server side. So, the intelligent human thought of the MVC programming pattern, that is, after the client submits the parameters-the server uses the servlet to retrieve the parameter information-and then the corresponding parameter calls the JavaBean method to implement the calculation-and then returns the result to the client.
Servlet Programming Basics Overview-mvc Control layer