[Rookie learns Java] 4: Servlet Technology
What is Servlet?
Servlet is a java language class used to expand the capabilities of the server through the request response mode. Although servlets can respond to any type of response, they are generally used to expand Web-based applications. In such applications, Java servlet technology defines specific HTTP servlet classes.
Generally, servlets run on multi-threaded servers. Therefore, you must understand that the Servlet must process concurrent requests and carefully synchronize access to shared resources. Shared resources generally include memory data (such as instances or class variables) and external objects (such as files, database connections, and network connections ).
Of course, here I will mainly discuss Servlets that generate responses to HTTP requests. Here, we have to say that an important class is HttpServlet. As mentioned above, it provides an abstract class that will be categorized by quilt to create an HttpServlet for a Web site, its subclass must override at least one method, which is usually a method such as doGet, doPost, and doDelete.
I may have said so much, but it is still not very clear about the role of Servlet. Maybe using diagrams can help you better understand Servlet. As shown in
From this we can see that Servlet is a small Java program running on the Web server. It usually receives and responds to requests from the Web Client through the Http protocol. So how to apply Servlet technology? See the code instance.
How to Use Servlet?
For clients, we use html pages for simulation. for servers, we write a simple Servlet program to process requests sent from web pages. The Code is as follows;
Login.html
LoginServlet, java code
Import java. io. *; import java. servlet. *; import javax. servlet. http. *; public class LoginServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username = request. getParameter ("username"); String password = request. getParameter ("password"); System. out. println ("username" + username); System. out. println ("password" + pa Ssword); response. setContentType ("text/html; charset = GB18030"); response. getWriter (). println ("Login successful !! ");}}
Web. xml Code
MyServlet
LoginServlet
MyServlet
/loginServlet
So what is the specific execution process of this Servlet program? The following sequence diagram can help you better understand it.
It can be seen that the Servlet receives user data from the client, processes the data internally or by calling an external program, and sends the processing result back to the client, thus completing the interaction between the user and the system, that's simple.
Summary:
Some programs are required on the server, often based on user input to access the database. These are usually completed using Common Gateway Interface (CGI) applications. However, running Java on the server can be implemented using the Java programming language. On servers with a large amount of communication, the advantage of assumervlet is that they run faster than CGI programs. Each user request is activated into a thread in a single program without creating a separate process. This means that the system overhead of the server to process the request will be significantly reduced.
In comparison, Servlet is better than CGI. It is mainly reflected in convenience, powerful functions, and good portability. It only requires a small investment and is relatively cheap. However, Servlet also has its own shortcomings, that is, it still uses the old CGI method to output a sentence, so it is very troublesome to write and modify HTML. The JSP and JSP + Bean technologies that emerged later are more convenient than Servlet, so Servlet may be used less now.