One, servlet development
The user enters a URL in the browser and returns, and the browser sends an HTTP request to the server. The server-side program accepts the request, processes the request, and then sends a response. The browser receives a response and then displays the content of the response. This request-response pattern is a typical Web application access process.
All request-response in the Javaweb application is done by the servlet. The servlet is the core program of Javaweb, and all URLs are eventually handed to the servlet for processing. The servlet does not have an execution method like main. When a user accesses a server, Tomcat accomplishes the entire process by invoking certain methods of the servlet.
What is a servlet?
A servlet program is a Java class that implements a special interface that is invoked and started by a Web server that supports Servlets. A servlet program is responsible for processing access requests for one or a set of URL addresses that it corresponds to, and receiving access request information from clients and generating response content.
The servlet program can accomplish most of the tasks that ordinary Java programs can accomplish:
1, gets the data that the client submits through the form form of the HTML and the parameter information after the URL
2, create the response information content to the client,
3, access the server-side file system
4, connect the database and develop database-based applications,
5, call other Java classes
Servlet Work Flow
The browser submits a request that follows the text of the HTTP protocol. This text is received and parsed by the server, Tomcat, and is encapsulated as a request object of type HttpServletRequest. All HTTP header data can be queried in the appropriate way by request. Tomcat also encapsulates the output stream as a response object of type HttpServletResponse, and the output can be controlled by setting the response object. In this process of request-response, Tomcat calls the request object, the response object as a parameter, and invokes the corresponding method of the servlet, such as Dopost (Request,response), Doget (Request,response) such as
Servlet interface
A servlet is a class that implements the Javax.servlet.Servlet interface. The servlet interface specifies a specific method to handle a particular request. The servlet specification is based on the HTTP specification. The HTTP 1.1 specification supports options, GET, POST, HEAD, PUT, DELETE, Trace, and other 7 access methods, the most common of which is GET and POST.
GET: Represents the query information, the URL can be accompanied by a small amount of parameter information, but the total URL length must not exceed 255 characters, and the parameters will be displayed in the browser address bar.
POST: Represents the submission information, generally used to submit big data information or files, submitted content is not limited by the length, and does not appear in the browser address bar.
Servlet Work Flow
The actual workflow for Web apps that are done with Servlets is through the Tomcat Server Publishing Service, and the interaction between the client and server follows the HTTP protocol. The specific workflow is as follows:
The client browser sends a request to the server side.
1, the server driven by the servlet container provided by the Tomcat server to resolve the received request.
2, through the Web application's configuration file, XML, to resolve to the corresponding execution of the servlet.
3,servlet completes the request logic sent by the client and completes the response sent to the client.
4, the servlet container provided by the Tomcat server responds to the client browser.
One, Java Web application design pattern:
1) Use of Jsp+javabean technology
JSP in response to the request and send processing results to the user, the data through the JavaBean to deal with, is the development of JSP technology is often used in the early stages of pattern 1, suitable for a number of small development projects.
2) using Jsp+javabean+servlet technology (MVC model)
MVC (Model-view-controller) model for medium or higher development projects
Model-View-Controller diagram:
MVC Architecture Design Diagram:
Second, the request forwarding mechanism in the servlet
In a Web application, a servlet is often used as the dispatcher's role, and such a servlet is called a controller that can forward requests to another servlet or JSP page, where they further process the request and produce a response to the request. How request forwarding is implemented in a servlet:
1) share a request:request.getRequestDispacher ("Xxx.jsp/xxxservlet"). ForWord (Requet,response);
2) Redirect:
Request.sendredict ("Xxx.jsp/xxxservlet");
Third, the steps to develop Web applications using the MVC pattern
1) define the bean to represent the data and encapsulate the business logic
A) define a data bean to represent the results that need to be displayed to the user
b) Define the business bean to encapsulate the business logic, Beandao.
2) Use the servlet to process user requests
A) writes the result of the data to the bean according to the user's request, and saves it in the context of the request or session
b) forward the request to the appropriate page in the form of a request forwarding, the servlet does not need to create any output, the output is done by the JSP page
2) JSP page output: Get data output from bean
Anyway:
①jsp interface
②servlet accepts user requests and makes initial processing, and then forwards the request
③ Business processing with beans
Jsp+javabean+servlet Technology (MVC model)