Document directory
- What is Servlet and JSP?
- MVC model
What is Servlet and JSP?
There are two main technologies used to develop Web applications using Java: Servlet and JSP. Servlet is a Java program executed on the server.Servlet ContainerThe Program (actually the server) is responsible for executing Java programs. While JSP (Java Server Page) is a PageJSP ContainerResponsible for execution.
The biggest difference between Servlet and JSP is that Servlet is mainly based on Java programs. when outputting HTML code, the out. println function must be used, that isEmbedded HTML in JavaJSP is mainly based on HTML pages. When Java code is required to be written, Java code is directly inserted into the page, that isEmbedded Java in HTML. A typical example is as follows:
JSP file
Copy codeThe Code is as follows: <Body>
<H1>
<% Out. println ("JSP"); %>
</H1>
</Body>
</Html>
ServletCopy codeThe Code is as follows: public class MyServlet ...{
...
Out. println ("Out. println ("<body> ");
Out. println ("
Out. println ("Servlet );
Out. println ("Out. println ("</body> ");
Out. println ("}
It can be seen that JSP is easy to output, while Servlet is easy to perform logic processing. Therefore, in practical applications, the two are often used in combination to perform their respective duties.
Tomcat is the one mentioned above.Servlet ContainerAndJSP Container. Each version supports the Servlet/JSP Protocol as follows:
Tomcat version |
Servlet support |
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 Tomcat is installed, you can check the webapps directory under the tomcat directory. This directory is used to save Web applications.
OfRoot directoryIs the directory that should be placed in the webapps directory. The WEB-INF directory cannot be accessed from a browser. The WEB-INF/classes stores the compiled Java program (mainly Servlet), The WEB-INF/lib stores the library file used during runtime (. jar file), web. xml is the configuration information of the entire application.
Other files, such as. jsp, images, and Javascript scripts, can be directly stored in the root directory of the Web application.
MVC model
The MVC model is a method for separating data, logical processing, and user interfaces.
- M (Model, Model): Used for data processing and logic processing.
- V (View, View): Displays the user interface.
- C (Controller, Controller): Control the logic trend and Screen Based on the client's request.
In Java, MVC corresponds to ans, JSP, and Servlet.
- M = JavaBeans: It is used to transmit data and has data-related logic processing.
- V = JSP: Receives data from the Model and generates HTML
- C = Servlet: Receives HTTP requests and controls Model and View
As shown in the following figure: