Servlet and JSP Technology is the Java Development Server-side application of the main technology, is the development of business applications, the standard of the presentation end. Java developers like to use it for a variety of reasons, one is familiar with the Java language developers are easy to learn the technology, the second is the "write, run everywhere" concept into the Web application, the implementation of the "write, everywhere." And more importantly, if you follow some good design principles, you can separate the presentation from the content, creating high-quality, reusable, easily maintainable and modified applications. For example, if you embed too much Java code (scriptlet) in an HTML document, you can create applications that are complex, difficult to read, easy to reuse, and difficult to maintain and modify later. In fact, in the CSDN jsp/servlet forum, often can see some questions, the code is very long, can be logical but not very clear, a lot of HTML and Java code mixed together, let people see confused. This is the abuse of random development.
If you have a basic understanding of the JSP and servlet technologies (preferably with some Web applications), we can explore some of the guidelines for how to develop "good" applications. Let's start by browsing the servlet and JSP technologies.
Servlet and JSP overview
The early dynamic Web pages mainly use CGI (Common Gateway Interface, Common Gateway Interface) technology, you can use different languages to write CGI programs, such as VB, C + + or Delphi. Although the CGI technology is mature and powerful, it has been replaced gradually because of the shortcomings of programming difficulty, inefficiency and complex modification. Among all the new technologies, Jsp/servlet has become more efficient, easier to program, more powerful, more secure and more portable, and thus is considered by many to be the most promising Dynamic Web site technology in the future.
Like CGI, the servlet supports the request/response model. When a client submits a request to the server, the server sends the request to Servlet,servlet responsible for processing the request and generating the response, which is then sent to the server and sent to the client by the server. Unlike CGI, the servlet does not generate a new process, but rather is in the same process as the HTTP server. It reduces the cost of the server by using threading technology. The servlet processes the request by invoking the service method when a request is received from the client, in which the servlet first determines what type of request is coming (Get/post/head ...). , and then calls the appropriate processing method (Doget/dopost/dohead ...). ) and generate a response.
Don't look so complicated, in fact, a servlet is simply a Java class. Unlike the general class, this class runs within a servlet container, providing session management and object Lifecycle management. So when you use the servlet, you can get all the benefits of the Java platform, including security management, access to the database using JDBC, and cross-platform capabilities. Also, the servlet uses threads, so that it can develop more efficient web applications.
JavaServer Pages (JSP)
JSP technology is a key technology of Java EE, it abstracts the servlet at a higher level. It allows regular static HTML to be combined with dynamically generated content that looks like an HTML Web page, but runs as a servlet. There are many commercial application servers that support JSP technology, such as Bea WebLogic, IBM WebSphere, JRun, and so on. Using JSPs is simpler than using a servlet. If you have a Web server that supports JSP, and you have a JSP file, you can put it in a position where any static HTML file can be placed, without compiling, without packaging, and without classpath settings, you can access it like a regular web page, The server will automatically help you do other work.
JSP working principle
The JSP file looks like a normal static HTML file, except that it contains some Java code. It uses the. jsp suffix to tell the server that the file needs special processing. When we visit a JSP page, the file is first translated by the JSP engine into a Java source file, which is actually a servlet, compiled, and then processed by the servlet engine like any other servlet. The servlet engine loads this class, processes requests from customers, and returns the results to the customer, as shown in the following illustration:
Figure 1: The process of invoking the JSP page