Home  >  Article  >  Java  >  Essential knowledge points for javaweb

Essential knowledge points for javaweb

angryTom
angryTomOriginal
2019-07-16 16:56:283361browse

1. WEB application

B/S (browser/server, browser/server) architecture

Based on HTTP transmission protocol (Hypertext Transfer Protocol, recall the name of HTML: Hypertext Markup Language)

WEB programs must run on web containers, such as Tomcat/Jboss/WebLogic, etc.

2. HTTP protocol

HTTP uses TCP as its supporting transport layer protocol, and the default port is 80 (default port).

Hypertext Transfer Protocol (HTTP for short) is an application layer protocol. HTTP is a request/response protocol, that is, after a client establishes a connection with the server, it sends a request to the server; after the server receives the request, it gives corresponding response information. ,

HTTP request message consists of four parts: request line, request header, blank line and request body. The following is a simple analysis of the request message format:

Request line: The request line consists of three parts: method field, URL field and HTTP protocol version field, separated by spaces. Commonly used HTTP request methods include GET, POST, HEAD, PUT, DELETE, OPTIONS, TRACE, CONNECT;

GET: When the client wants to read a resource from the server, use the GET method . The GET method requires the server to put the resource located by the URL in the data part of the response message and send it back to the client, that is, to request a resource from the server. When using the GET method, the request parameters and corresponding values ​​are appended to the URL. A question mark ("?") is used to represent the end of the URL and the beginning of the request parameters. The length of the passed parameters is limited. For example, /index.jsp?id=100&op=bind.

POST: The POST method can be used when the client provides a lot of information to the server. The POST method submits data to the server, such as completing the submission of form data and submitting the data to the server for processing. GET is generally used to obtain/query resource information, and POST comes with user data and is generally used to update resource information. The POST method encapsulates the request parameters in the HTTP request data, appearing in the form of name/value, which can transmit a large amount of data; Request header: The request header consists of keyword/value pairs, one pair per line, keywords and values ​​are used Separated by English colon ":".

The request header informs the server about the client's request. Typical request headers are:

User-Agent: The type of browser that generated the request;

Accept: List of response content types recognized by the client; asterisk "*" is used to group types by range, use "*/*" to indicate that all types are acceptable, use "type/*" Indicates all subtypes of the acceptable type type;

Accept-Language: the natural language acceptable to the client;

Accept-Encoding: the encoding acceptable to the client Compression format;

Accept-Charset: acceptable response character set;

Host: requested host name, allowing multiple domain names to be in the same IP address, That is, virtual host;

connection: connection method (close or keepalive);

Cookie: stored in the client extension field, and sends information belonging to the server to the same domain name Domain cookie;

Empty line: There is an empty line after the last request header, and carriage return and line feed characters are sent to notify the server that there are no more request headers below;

Request package body: The request package body is not used in the GET method, but used in the POST method. The POST method is suitable for situations where customers are required to fill out a form. The most commonly used ones related to the request package body are the package body type Content-Type and the package body length Content-Length.

The following is a simple analysis of the response message format:

Status line: The status line consists of the HTTP protocol version field, status code and status code The description text consists of 3 parts, separated by spaces; the status code consists of three digits, the first digit indicates the type of response. There are five categories of commonly used status codes as follows:

1xx : Indicates that the server has received the client request, and the client can continue to send requests;

2xx: Indicates that the server has successfully received the request and processed it;

3xx: Indicates that the server requires the client to retry Direction;

4xx: Indicates that the client's request contains illegal content;

5xx: Indicates that the server failed to process the client's request normally and an unexpected error occurred;

status The code description text has the following values:

200 OK: Indicates that the client request is successful;

400 Bad Request: Indicates that the client request has a syntax error and cannot be understood by the server;

401 Unauthonzed: Indicates that the request is unauthorized, this status code must be used together with the WWW-Authenticate header field;

403 Forbidden: Indicates that the server received the request, but refused to provide the service, usually Give the reason for not providing the service in the response body;

404 Not Found: The requested resource does not exist, for example, the wrong URL was entered;

500 Internal Server Error: Indicates that the server has Unexpected error, resulting in failure to complete the client's request;

503 Service Unavailable: Indicates that the server is currently unable to process the client's request. After a period of time, the server may return to normal;

3. Servlet

Servlet is the core of javaWEB. Simply put, it is a server-side program written in Java. When the user makes a dynamic request (a static request is a direct request for HTML), it is actually requesting a servlet

WEB container loads the Servlet into the memory and initializes it through the init method

Service() method The corresponding processing method doPost() or doGet() is called according to the request method, and there are some other request methods doPut() doOptions()... But these methods are not commonly used and are recommended to be blocked from a security perspective.

When the Server no longer needs the Servlet (usually when the Server is shut down), the Server calls the Servlet's destroy() method.

4. JSP

Java Server Pages is a technology that mixes static coding machines and dynamic coding Java. It is also said that java The code is embedded in HTML. Before JSP, Servlet was the main body, with relatively powerful functions and advanced design. However, the HTML page was completely Java's out.print(), which outputs line by line, which is very important for page writing and modification. It was very inconvenient, which prompted SUN to launch JSP. JSP, like ASP and PHP, are all embedded languages. In addition, when the WEB container is moved, JSP will be compiled into Servlet. It is also said that JSP is an upgraded version of Servlet.

5. Tomcat

There are many Java WEB containers. Here is Tomcat as an example. As mentioned earlier, WEB programs must run on WEB containers. Tomcat is used as a WEB container to run Java WEB programs. Tomcat is written in JAVA language and requires JVM as the running environment.

Tomcat’s directory structure

bin……………………………… executable program (script), start and stop debugging some bat.sh of Tomcat

conf……………………………….Configuration file webx.ml server.xml etc.

lib…………………………………….jars required to run Tomcat Package

logs………………………….Log file

temp…………………………………….Temporary file

webapps………………WEB program (compiled project file)

work……………………………….generate jsp files into java files in this directory

As a supplement, Tomcat does not compile jsp into Java files in real time, so sometimes the page is modified and it does not take effect in time. At this time, we only need to delete the corresponding files in the work directory.

6. Java WEB project structure

Generally speaking, the Java projects we often talk about are basically WEB projects (B/S Architecture), it’s not that Java can’t do C/S programs. In fact, Java’s Swing is used to draw interfaces. But now C# has become the most popular language for drawing interfaces. It is fast and simple, so fewer and fewer people use Java to do it. C/S program developed. We mainly talk about Java WEB projects. The following is the most basic project structure.

src………………………….JAVA source code, java file directory

webContent/webRoot…………project release directory

WEB- INF

lib………………………………………….jar package required for the project

web.xml……………… Entrance to the WEB program

The above is the core structure of the Java WEB project. In actual development, building the project will be more complicated than these, especially after the framework is referenced, the addition of configuration files will add some new directories.

7. Development Framework

#As mentioned earlier, when the client initiates a dynamic request, the request goes to the Servlet for processing, and then returns, and the entire request is processed All the work is handled in Servlet, which seems relatively simple. Every time a different request is needed, a Servlet is written. However, in actual development, if there are many functional modules, we need to write many Servlets, and if we want to modify The return of a request must also be achieved by modifying the Java code (Servlet).

This is more troublesome for function expansion and maintenance, so it has promoted the popularity of many integrated and concurrent development frameworks, such as Webwork, Struts, SpringMVC, Jfinal, etc., which are all mainstream Java WEB development frame.

There is a very general overview of the advantages of using a framework. It can speed up the development process. Reusing code in similar projects will save developers a lot of time and energy. Frameworks provide pre-built modules for performing tedious coding tasks. Let users only care about the development of core business, and the framework helps you shield various technical issues that have nothing to do with the original technology and business development. But if you want to understand a framework in depth and use it more skillfully, you still need to understand the underlying principles and find its source, which is Servlet.

Take SpringMVC as an example to briefly describe the working principle

SprfngMVC mainly implements the Servlet interface through DispatcherServlet (Servlet dispatcher, configured in web_xml), also called Front-end controller, requests from the front-end will arrive here first, and it is responsible for matching the appropriate handler to the background. The main workflow of DispatcherServlet is as follows:

1. The client sends an http request to the web server, and the web server parses the http request. If it matches the request mapping path of DispatcherServlet (specified in web.xml), the web container will transfer the request to DispatcherServlet.

2. After receiving this request, DipatcherServlet will find the processor Controller (Handler) that handles the request based on the request information (including URL, Http method, request header and request parameter Cookie, etc.) and the configuration of HandlerMapping.

3. DispatclierServlet finds the corresponding Handler according to HandlerMapping, hands the processing power to the Handler (the Handler encapsulates the specific processing), and then the specific HandlerAdapter makes a specific call to the Handler.

5. After Handler completes data processing, it will return a ModeAndView object to DispatcherServlet.

6. The ModelAndView returned by Handler is only a logical view and not a formal view. DispatcherSevlet will use ViewResolver to The edit view is converted into a real view (this sentence simply returns a relative path to the page to be returned, and is converted to the specific page through the try parser).

7. Dispatcher parses the parameters in ModeAndView through the model and finally displays the complete view and returns it to the client.

8. Use SpringMVC to write functional modules

Because the workload of framework configuration is relatively large, you also need to have an understanding of various technologies, so I will not describe it for the time being. Let us briefly talk about how to complete a functional module based on the existing framework in actual development and understand the basic process of the function. Take login as an example:

1. First, there must be Login portal (JSP page), in the SpringMVC framework we do not recommend direct access to the JSP page, so access to our login page is also processed through the background;

2. Write the 丨ogin.jsp page and the login return page index .jsp ;

3. Write LoginController, which contains two request control methods, one preLogin() and one doLogin(). PreLogin() is responsible for processing the request for the login page (return to login.jsp) , doLogin() processes the login request and returns to index.jsp.

4. Login request needs to be made through form or ajax in login.jsp.

Request/preLogin business logic processing returns to login.jsp à Enter the user name and password...

Request/doLogin business logic processing returns the login result index.jsp.

9. Other basic knowledge

Database operations, Jdbc/Hibernate/Mybatis

JSP tags, EL expressions, Struts tags, C tags, etc.

Basic front-end technology CSS/JS/jQuery/Ajax

The above is the detailed content of Essential knowledge points for javaweb. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn