JAVAWEB-Tomcat installation and Eclipse and IDEA configuration and Http protocol

Http protocol

1. What is Http protocol

HTTP, HyperText Transfer Protocol (HyperText Transfer Protocol) is the most widely used network protocol on the Internet. All WWW documents must comply with this standard. The original purpose of designing HTTP is to provide a way to publish and receive HTML pages

2. Http protocol composition

Http protocol byHttp requestwithHttp responseComposition, when you enter a URL in the browser to visit a website, your browser will encapsulate your request into an Http request and send it to the server site. After receiving the request, the server will organize the response data and encapsulate it into an Http response and return it to Browser. That is, there is no response if there is no request.

Three, Http request

We visit a website casually. get onCaptureOperation (three slashes in the upper right corner of the Firefox browser -> web developer -> network) can see the content of the captured packet.

Http request. Consists of three parts.http request linehttp request headerhttp request body

Http request analysis:

Http request line

  • Request method: POST, GET
  • Resource requested: /http://event.csdn.net/logstores/...
  • Protocol version: HTTP/1.1
    • HTTP/1.0, send a request, create a connection, obtain a web resource, and disconnect.
    • HTTP/1.1, send a request, create a connection, get multiple web resources, and keep the connection.

Http request header
The request header is some information sent by the client to the server, using key-value pairs to represent key: value

Insert picture description here
Introduce the content of several common request headers:

Referer The browser informs the server where the current request comes from. If it is a direct visit, there will be no such header. Commonly used: anti-leech
If-Modified-Since The browser notifies the server of the last change time of the local cache. Combine with another response header to control the caching of the browser page.
Cookie Technology related to sessions, used to store cookie information cached by the browser.
User-Agent The browser informs the server, client browser and operating system related information
Connection Stay connected. Keep-Alive connection, close is closed
Host Requested server hostname
Content-Length Length of request body
Content-Type If it is a POST request, there will be this header, the default value is application/x-www-form-urlencoded, which means that the request body content is url-encoded
Accept The MIME types supported by the browser. A description of the file type. MIME format: large type/small type [;parameter] For example: text/html, html file text/css, css file text/javascript, js file image/*, all picture files
Accept-Encoding The browser informs the server of the data compression format supported by the browser. Such as: GZIP compression
Accept-Language The browser notifies the server of the language supported by the browser. National languages ​​(Internationalization i18n)

Request body

  • When the request method is post, the request body will have the requested parameters in the following format:
    username=zhangsan&password=123
  • If the request method is get, then the request parameters will not appear in the request body, but will be spliced ​​behind the url address
    http://localhost:8080...?username=zhangsan&password=123

Four, HTTP response

Http response. The same three parts.http response linehttp response headerhttp response body

Http response analysis:

http response line

Insert picture description here

  • Status code: Description
    of the status The commonly used status codes are as follows:
    200: The request is successful.
    302: Request redirection.
    304: The requested resource has not changed, and the local cache is accessed.
    404: The requested resource does not exist. Usually the user path is written incorrectly, or the server resource may have been deleted.
    500: Internal server error. Usually the program throws an exception.
  • Status information: Status information changes according to the status code changes

Response header

Responses are also in the form of key-value pairs, and the server returns information to the client in the form of key-value pairs.

Common request headers description
Location Specify the response path, which needs to be used in conjunction with the status code 302 to complete the jump.
Content-Type The type of response body (MIME type) value: text/html;charset=UTF-8
Content-Disposition Analyze the text value by downloading through the browser: attachment;filename=xx.zip
Set-Cookie Technology related to conversation. The server writes a cookie to the browser
Content-Encoding The value of the compression format used by the server: gzip
Content-length Response body length
Refresh Regular refresh, format: seconds; url=path. The url can be omitted, and the default value is the current page. Value: 3;url=www.itcast.cn //Refresh the page to www.itcast.cn in three seconds
Server Refers to the server name, default value: Apache-Coyote/1.1. It can be modified through conf/server.xml configuration. <Connector port=“8080”… server=“itcast”/>
Last-Modified The server informs the browser of the last modification time of the file. Used with If-Modified-Since.

Tomcat server

1. Common concepts in web development

  • B/S system and C/S system
    Brower/Server: browser server system ----- website
    Client/Server: client server system ----- QQ, Feiqiu, large game

  • web application server
    Server software for publishing web resources to the outside
    Insert picture description here

  • Web resources The resources
    that exist in the web application server and can be accessed by the outside world are web resources.
    For example: Html, Css, js, pictures, videos, etc. that exist in the web application server

    1) Static resources: Refers to the data in the web page for people to browse is always unchanged. For example: HTML, CSS, JS, pictures, multimedia.

    2) Dynamic resources: refers to the data in the web page for people to browse is generated by the program, and the content seen by visiting the web page at different time points is different. For example: JSP/Servlet, ASP, PHP

JavaWEB field: dynamic resources think that html is dynamically generated through java code

What you see on the web page varies. For example: JSP/Servlet, ASP, PHP

JavaWEB field: dynamic resources think that html is dynamically generated through java code

Two, web application servers commonly used in web development

  • weblogic: Oracle’s large fee-based web server supports all javaEE specifications
  • websphere: IBM’s large fee-based web server supports all javaEE specifications
  • Tomcat: The open source free small and medium-sized web application server under the Apache open source organization supports the servlet and jsp specifications in javaEE

Three, Tomcat download and installation

  1. Download Tomcat
    official website address: http://tomcat.apache.org/whichversion.html
  2. Install Tomcat
    Tomcat has an installation version and a decompression version (green version). The
    installation version is an installation package in the form of .exe. Double-click to install on our computer. The
    decompression version is less used , that is, the green version. Use it directly after decompression. many
    Insert picture description here
  3. Tomcat directory structure

Insert picture description here
bin: script directory

  • Startup script: startup.bat
  • Stop script: shutdown.bat

conf: Configuration file directory (config /configuration)

  • Core configuration file: server.xml
  • User authority profile: tomcat-users.xml
  • Default configuration file for all web projects: web.xml

lib: Dependent library, jar package used in tomcat and web projects
logs: log file.

  • localhost_access_log. .txt tomcat records user access information, and the star indicates the time.
    For example: localhost_access_log.2016-02-28.txt

temp: Temporary file directory, the contents of the folder can be deleted at will.

webapps: The directory where the WEB project is published by default. Projects written are run in this directory.

work: The working directory for tomcat to process JSP.

4. Tomcat startup and operation

Double-click startup.bat under bin under Tomcat to start Tomcat

Enter http://localhost:8080 in the address bar of the browser and see the page to prove that the startup is successful.

Analysis of the reasons for the unsuccessful startup of Tomcat:
1) If the JAVA_HOME environment variable is not configured, when you double-click the "startup.bat" file to run tomcat, it will shut down immediately. And must be configured correctly, and JAVA_HOME points to the JDK installation directory

2) Port conflict
java.net.BindException: Address already in use: JVM_Bind: 8080
modificationTomcat/conf/server.xml

Insert picture description here

Tomcat server and Eclipse configuration

Step 1:
Open eclipse->windows->Find server
Insert picture description here
Step 2:
Click add to find our Tomcat version
Insert picture description here
Step 3:
Find Tomcat
Insert picture description here
Step 4:
First open the Servers view
Insert picture description here
Step 5: Double-click the configured Tomcat. We need to publish the project to the file directory of webapps under Tomcat and
Insert picture description herethen find the second one, and find the webapps under Tomcat in the output path

Insert picture description here

IDEA and tomcat configuration

  1. New WEB project
    Insert picture description here

  2. Create two new folders under the web/WEB-INF directory, classes are used to store the bytecode files (.class) of the servlet, and lib is used to store the packages referenced by the project.
    Insert picture description here

  3. Enter Project Structure, enter the Modules (IDEA project) tab, and change the two output paths of Paths to the newly created classes in step 2.
    Insert picture description here

  4. Then click Dependencies, select the + sign on the right, create a new JARS path, and select the lib folder created in step 2
    Insert picture description here
    Insert picture description here

  5. Enter the Artifacts tab and set the output directory as the newly created project folder under webapps in the Tomcat installation location
    Insert picture description here

  6. Run->Edit Configurations configure Tomcat, which is usually configured by default
    Insert picture description here

  7. Set the name of the WEB resource, it can also be "/", empty
    Insert picture description here

  8. Configure web.xml and write Servlet. Note that
    Insert picture description here
    you can use annotations to access sevlet

Guess you like

Origin blog.csdn.net/Mr_GYF/article/details/109073982