Java Web Basics Tutorial (ii) Development fundamentals reprint: Future Wei Lai
Objective
The Java Web is a technical implementation of a network application based on the b\s (browser \ Server) architecture. This structured Web application is used by a large number of enterprise-class applications. As the scale of Web applications grows, the traditional Web development model cannot meet the increasingly complex business needs, so there is a more reasonable and more advanced MVC development model. There is also a series of development frameworks based on the MVC pattern, struts framework, Spring MVC and so on.
1. Development of WEB Technologies
1. First stage static website
At the beginning of web technology, due to the limitations of hardware technology most Web applications are static-based HTML pages, and some dynamic Web applications are not as smooth as they are now. Static pages can only serve as information releases, and they do not have the ability to interact.
2. Phase II Dynamic website
Applications that do not have interactive capabilities are definitely not popular. Many experts have proposed different solutions for implementing dynamic Web programs. One of the first things that was proposed was the CGI (Universal Gateway Interface), the most important of which was the ability to access the database through CGI, since almost all Web applications required access to the data. Although CGI technology is very powerful, but it also has its own shortcomings, such as the implementation of CGI program is relatively difficult technical, code difficult to maintain and so on.
3. Servlet implements Dynamic Web site
To address these legacy issues, Java introduced its own dynamic Web technology Servletin 1999.
Servlets have the following technical advantages
- Implementation based on Java technology
- Cross-platform
- Access to a wide range of databases
- Multithreading
- Internet
Request response Process for servlet
- The browser sends a request to the server
- Web server accepts requests
- Submit a request to the servlet engine
- The servlet engine calls the service method
- Returns the response to the client (corresponding code)
The servlet is the core of the entire Javaweb technology, meaning that the servlet can implement all the functions of the dynamic Javaweb program. However, if the logic of the client page is very complex, the workload of writing the servlet is very large and the code is difficult to maintain.
4. The advent of JSP
In order to solve this problem to implement the page display code writing and control logic code separation,JSP technology was born.
JSP technology can be used to mix client code and server-side code in the same page, you can directly use Java code as a server-side script.
In order to make full use of the existing servlet technology, the JSP page is translated into a servlet by the JSP engine and then processed by the servlet engine when the JSP page is accessed for the first time.
JSP page request, corresponding and run Flow 2. Java Web Technology
1. Basic components of the Java Web
A typical Java Web application should contain the following elements, package them together, and run them in a Web container.
- Servlet
- Java Server Pages
- JSP standard tags (JSTL) and custom labels
- Java classes for use in Web applications
- static files, including HTML, images, JavaScript, and CSS
- Describes meta information for Web applications (XML)
2. Directory structure of the Java Web
Standard directory structure
- UNTITLED3 is the root directory of this project.
- Web directory to store resource files such as Picture directory, CSS directory, JS directory, JSP files, etc.
- Web-inf directory for project configuration file Web. xml and Lib library files
3. Java Web configuration file
The configuration file is the backbone of all Java Web applications. The file we are introducing here is primarily the Web. xml file located in the Web-inf directory. In addition to the tomcat\conf directory there is also a Web. xml file, which is a configuration file for the Tomcat server, a global configuration.
The configuration file can be configured as follows??
- Servlet Context Initialization Parameters
- Session Configuration
- servlet/jsp definition
- Tag Library Reference
- MIME type mappings
- Welcome page
- Error page
- Security information
3. MVC Pattern and MVC framework
After extensive use of servlet/jsp technology, developers have gradually discovered the drawbacks of servlet and JSP technology.
HTML code, JavaScript, CSS and dynamic jstl, Java code mixed together is not conducive to post-maintenance, in order to solve the problem of the page and server-side code separation, which is the basic idea of the MVC pattern.
1. JSP Model 1 and JSP Model2
Mode1 is made up of three parts
- client browser
- Server-side program (Jsp/servlet)
- Database
The structure of Model1
This structure also has its shortcomings, no further code-level separation.
MVC is the abbreviation for model-View-controller
Here is the interactive diagram
The structure of Model2
The MVC pattern allows for maximum code reuse
2. Basic services in Web applications
There are many basic features that can be reused on many pages in a Web application, and we summarize the following.
- Page navigation: This feature is best implemented in the controller
- Page layouts: Using standard methods to implement page layouts
- Data validation: The best way to validate data through a collection framework
- Business logic reuse: Separating the reusable business logic from the system and forming a series of independent components makes it easy to reuse.
3. What is the MVC pattern?
The MVC pattern divides an application into 3 parts: model, view, controller, and these 3 components should be coupled as little as possible, thus improving the scalability and maintainability of the application.
- Multiple views can correspond to multiple models
- You can provide different data for a view through the model
- You can add permission validation to a controller to restrict user access to sensitive resources
- It is advantageous to extend the layers in the system by reducing the coupling between each level.
4. Common MVC Framework
Struts 1.x
Struts 2.x
Spring MVC
Summarize
We understand the history and development of web technologies, understand what is necessary in Web application development, and understand the important role of the MVC model in multi-person development.
Next section Trailer
- Html
- Css
- Javascript
- Ajax
Please look forward to the OH ~
Java Web Basics Tutorial (ii) Development fundamentals