Summary of basic knowledge of JavaWeb

First on a JavaWeb knowledge framework diagramInsert picture description here

1. Web application server and Http protocol

(1) Web overview

Web resources are divided intoStatic resources(HTML) sumDynamic resources(Jsp, php, etc.) Compared with static resources, dynamic resources can realize interaction with users. JavaWeb is the collective name for dynamic web resource development technology.

(2) Overview of Tomcat

Tomcat is an open source small web server, it can only support the development of small and medium-sized web projects, and only supports a small number of JavaEE specifications such as Servlet. It is a product under the Apache organization and is completely free. In the process of using Tomcat, it is often integrated with programming software such as IDEA.

(3) Http protocol

Http protocol is hypertext transport protocol, which is used to define the specification of data exchange between web browser and web server. A complete conversation includesRequest(Request message) andResponse(Response message). The http protocol defines a variety of request methods, among whichgetversuspostIt is the most common request method.

Get request related : Generally, when a browser directly accesses an address (URL), it will be a get request by default. The characteristic of a get request is: when submitting a request, the submitted information will be directly carried in the request line and the data submitted by the get request Generally smaller .

Post request related : The realization of a post request is generally completed through a form. The characteristic of a post request is that the information submitted by the user will be in the body, which is more secure than that, and the maximum amount of data allowed to be transmitted is also relatively large .

Then there is the common response message status code :
1xx Nothing is done directly return
2xx successfully return
3xx Some things have been done but not all completed.
4xx client error
5xx server error
especially:
200 correct
302 redirect
304 page has not changed
404 page not found
500 server error.

2. Overview of the three major components of JavaWeb

(1)Servlet

The servlet definition is ainterface. In my understanding, servlet should be regarded as the core of a JavaWeb project, because servlet is actually used to meet the functions required by users.

Since it is an interface, it cannot be used directly in actual use. Generally we inherit its implementation classHttpServletTo use, and need to be inweb.xmlConfigure relevant information in the. In the newer tomcat version, we can passannotationConfigure the declaration directly to the servlet.

Use of HttpServlet: HttpServlet provides some Http-related functions. In actual use, we only need to cover and rewrite it.dogetwithdopostmethod.

Three servlet domain objects :
ServletContext: the entire project scope
HttpSession: session scope
HttpServletRequest: request scope The
domain object can be used toAttribute(Attribute) setting, reading and deleting. Note: ServletContext is created when the Web container is created, and will not die until the container is closed. Only one ServletContext object can exist in the entire project.

Servlet details :
1. The servlet isSingletonOf and alsoThread unsafeof. So in actual use, we try not to create member variables as much as possible.
2. The servlet can be created when the first request is made, or when the container starts. The default is to create on the first request.

(2)Filter

Filter translation isfilter. It is used to implement interception during the interaction between the client and the server, and will be released if and only when the conditions are met. Can be used according to user identity or statusGive different permissions, Can also be used toSensitive character filteringWait. In fact, Filter is very similar to Servlet, and it is also an interface. You still need to write its implementation class when using it.

Note: Four interception methods of Filter:
request: intercept direct request method
forward: intercept request forwarding method
include: intercept request include method
error: intercept error forwarding method

(3)Listener

Listener translation isListener. Used to perceive various operations and attribute changes during project execution. Compared with the first two, the importance of listener is reduced.

三.Request&Response

(1) Request overview

When the client makes a request, tomcat will create request and rsponse to call the ServletserviceMethod, each request will create a new request and response. The request is used to complete the request message from the client to the server.

Request frequently used functions :
1. Get the request message (especiallygetParameterwithgetParameterValuesMethod)
2. Set the request code (setCharacterEncodingMethod)
3. Request forwarding (request.getRequestDispatcher(“/**”).forward(request,response)Complete forwarding)
Note: Because request is also a domain object, another servlet in the project uses the same request object when forwarding, so the two can share the data in the request domain. This needs to be distinguished from redirection.

(2) Overview of Response

The function of Response corresponds to Request, which is used to complete the response message from the server to the client.

Response common functions :
1. Get character or byte stream (getWriterwithgetOutputStreamMethod)
Note: The two cannot exist at the same time within the same request range
2. Set the character encoding (setCharacterEncodingMethod)
3. Set up automatic jump page (eg:response.setHeader(“Refresh”, “5;URL=http://www.baidu.com”) Means automatically jump to Baidu page after 5 seconds)
4. Set error status code (eg:response.sendError(404, "The resource you visited was not found"))
5. Redirect (response.sendRedirect(“/***”)

4. Conversation technology

(1)Cookie

Cookies are made byServer creation, And then send a key-value pair to the client in response.The client saves cookies, And will indicate the source of the cookie. When the client sends a request to the server, it will include all the server cookies in the request and send it to the server, so that every request operation of the user can be recorded.

Also because the cookie is stored on the client, we can also view the cookie directly on the client:

Insert picture description here

(2)Session

The bottom layer of Session isDepends on cookiesof. Session is also created by the server, whileSave in server. The server will give the client an id when creating a Session. When the client accesses the server again, it will bring the sessionId in the request, and the server will find the corresponding session through the sessionId.

In actual use, an important application of Session isVerify client verification code

Note: Session consumes a lot of memory in the server, so in general, there is only one Session in a session. At the same time, Session also has an automatic deletion mechanism, which is 30 minutes by default in tomcat. That is to say, the last Session has been deleted after the client has no operation for more than 30 minutes .

5. Overview of JSP&EL&JSTL

JSP implementedHTML markup language and Java codeCo-authored. EL expression is for JSPSimplification of Java code, And JSTL is aStandard tag library

Six. Database

(1 Overview

A database is a warehouse used to store and manage data. The database is divided intoRelational and non-relational databases. CorrectRelational database, There is a degree of coupling between its internal data, and its data is stored inhard diskUp. andFor non-relational databases, Its internal data isKey-value pairExists in the form of data stored inRAM, And then solidify to the hard disk at regular intervals, which greatly improves the query and modification operations compared with relational databases.effectiveness

(2) MySQL database

MySQL database is an open source free database software owned by Oracle, which is a relational database. The statements that operate most relational databases are SQL statements.

Commonly used SQL statements :
1. Insert (eg:INSERT INTO stu(sid, sname,age,gender) VALUES(‘99’, ‘zhangSan’, 18, ‘male’))
2. Modification (eg:UPDATE stu SET sname=’liSi’, age=’20’ WHERE sid>50 AND gender=’male’)
3. Delete (eg:DELETE FROM stu WHERE sid=‘99’)
4. Query (eg:SELECT * FROM stu where sid=‘99’ and age=‘18’)
Note: Taking into account the security issues of SQL injection during actual use, user parameters are generally represented by placeholders?

(3) Redis database

Redis database is an open source free non-relational database under the Apache organization. It can store many types of data, namelyFive types: String type, Hash (map) collection, List collection, Set collection and ordered Set collection

Operating non-relational databasesThere is no general statement, Every non-relational database has its own completely different grammar specifications. In Redis, the code for the same operation for each data type is also different. eg: The code for adding String type data is set(***,***), while adding Set type is sadd(***,***).

七. JDBC&Jedis

(1) Overview of JDBC

JDBC is defined by SUMA set of specifications (interface) for operating the database with Java code. Each database manufacturer is responsible for implementing the API to connect to its own database according to the JDBC interface.
In order to simplify the code of operating JDBC and improve operating efficiency, we usually useDatabase connection pool. At the same time, in order to ensure the correctness of the program, we useJDBC transaction management.
To continue to simplify the writing of Java code, we often write and useDBCUtis tools. Similar to this, we often write and use BeanUtils tool classes.

(2) Overview of Jedis

Jedis, similar to JDBC, is also used to implement Java code operations on the database. JedisUsed for operations on Redis databaseCompared with JDBC, Jedis's operation is much simpler. Java code is exactly the same as Redis's database operation statement.

8. Maven project management tool

Guess you like

Origin blog.csdn.net/m0_46550452/article/details/104988241