What are servlet and JSP
There are two main technologies used to develop Web applications in Java, namely, Servlet and JSP. A servlet is a Java program that executes on the server side, and a program called a servlet container (in fact, a server) is responsible for executing Java programs. The JSP (Java Server page) is a page that is responsible for execution by the JSP container .
The biggest difference between a servlet and a JSP is that a servlet is primarily a Java program, requiring the use of the OUT.PRINTLN function when exporting HTML code, that is, embedded HTML in Java, and JSP for HTML pages. When you need to write Java code, you insert Java code directly into the page, which is embedded in HTML. A typical example is as follows:
JSP file
Copy Code code as follows:
<body>
<% out.println ("JSP"); %>
</body>
Servlet
Copy Code code as follows:
public class Myservlet ... {
...
Out.println ("Out.println ("<body>");
Out.println ("
Out.println ("Servlet");
Out.println ("Out.println ("</body>");
Out.println ("}
Visible, the JSP is easy to output, and the servlet facilitates logical processing. Therefore, the practical application of the two are often used in conjunction with their respective roles.
Tomcat is the servlet container and the JSP container mentioned above. The SERVLET/JSP protocol support for each version is as follows:
Tomcat version |
Support Servlet |
Support JSP |
6.0.x |
2.5 |
2.1 |
5.5.x |
2.4 |
2.0 |
4.1.x |
2.3 |
1.2 |
3.3.x |
2.2 |
1.1 |
Structure of Web applications
After the Tomcat is installed, you can look at the WebApps directory in the Tomcat directory. This directory is used to save the Web application.
The root of the image above is the directory that should be placed in the WebApps directory. The Web-inf directory cannot be accessed from the browser. web-inf/classes Saves the compiled Java program (mainly the servlet),Web-inf/lib saves the library file (. jar file) that is used by the runtime, andweb.xml The configuration information for the entire application.
Other files, such as. JSP, pictures, JavaScript scripts, and so on, can be placed directly under the root directory of the Web application.
MVC model
The so-called MVC model is a method of separating data, logic processing and user interface.
- M (model, models): For data processing, logic processing.
- V (view, views): Used to display the user interface.
- C (Controller, Controller): According to the client's request to control the logical direction and screen.
In Java, the three parts of MVC correspond to JavaBeans, JSPs, and servlet respectively.
- M = JavaBeans: Used to pass data, with data-related logical processing.
- V = JSP: Receive data from model and generate HTML
- C = Servlet: Receive HTTP request and control model and view
The drawings are as follows: