Personal homepage: Hello Code.
This article column: "Java WEB from entry to actual combat"
Please click to go to Java WEB from entry to actual combat for the complete content of Java WEB. If you
have any questions, please correct me and learn together~~
Article directory
Nginx
- Nginx is a server software
- Its core function is that it can be combined with server hardware, so that the program can be published to the Nginx server, allowing more users to browse
Install
-
Upload compressed package:
put nginx压缩包位置
(press alt+p in CRT to enter sftp) -
Unzip the compressed package:
tar -zxvf 压缩包名
-
Enter the decompression directory:
cd nginx解压目录
-
Install dependencies
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum -y install openssl openssl-devel
yum -y install gcc
-
Install nginx
./configure
make
make install
(there will be an nginx directory under /usr/local/ after installation) -
Enter the corresponding directory:
cd /usr/local/nginx/sbin
-
Start nginx service:
./nginx
stop:./nginx -s stop
restart:./nginx -s reload
-
Check the nginx service status:
ps -ef | grep nginx
-
Test the nginx service: the browser opens the corresponding Linux server ip address
Finally, open the corresponding ip address in the browser and cannot access it. The solution:
the first step, configure the firewall for port 80: thefirewall-cmd --zone=public --add-port=80/tcp --permanent
second step, restart the firewall service:systemctl restart firewalld.service
then re-visit your ip in the browser, it should be able to access .
Publish the project
-
exist/homeCreate a web directory under:
mkdir web
-
Upload the project to this directory:
put web项目压缩包
-
Unzip the project archive:
unzip web程序压缩包
-
Edit the nginx configuration file:
vim /home/nginx-1.18.0/conf/nginx.conf
find the curly bracket range of the server and modify the path of the location
-
Enter the corresponding directory:
cd /usr/local/nginx/sbin
-
Shut down the nginx service:
./nginx -s stop
-
Start the nginx service and load the configuration file:
/usr/local/nginx/sbin/nginx -c/home/nginx-1.18.0/conf/nginx.conf
-
Test the website through your browser
Introduction to Enterprise Development
JavaEE Specification
- JavaEE (Java Enterprise Edition): Java Enterprise Edition
- It is an industry standard that is led by SUN and jointly developed by various manufacturers and is widely recognized.
- JavaEE was called J2EE in the early days, but it did not continue to use its naming convention. The version of J2EE starts from 1.0 to the end of 1.4. The JavaEE version starts from the JavaEE 5 version, and the latest version is JavaEE 8.
- The JavaEE specification is an umbrella term for many Java development techniques. These technical specifications are inherited from J2EE. A total of 13 technical specifications are included.
- 包括:JDBC, JNDI, EJB, RMI, IDL/CORBA, JSP, Servlet, XML, JMS, JTA, JTS, JavaMail, JAF。
WEB overview
Overview
- WEB in the computer field representsThe internet。
- Like the WWW we used before, it's an acronym for the three-word World Wide Web, called:World Wide Web
- The emergence of network-related technologies is to allow us to live in a networked worldAccess to resources, where these resources are stored, we call itwebsite。
- By entering the address (URL) of the website, we can access the resources provided in the website (without distinguishing between local area network and wide area network)
Resource classification
- Static resources
The resources provided to people on the website are immutable, that is to say, different people or at different times, see the same content.As developers, the HTML, CSS, and JavaScript we write are all static resources.
- Dynamic resources
The resources displayed to people on the website are generated by programs, and different people see different content at different times or due to different identities.As a website developer, the JSPs and Servlets we write are all dynamic resources.
system structure
In the previous study, all Java projects were developed. These projects are called projects or products in the enterprise. It's all about the system!
Infrastructure division
- CS structure: (Client Server)Client + Serverway (installing different applications directly on the client)
- BS structure: (Browser Server)browser + serverThe way
Technology selection division
- Model1 model
- Model2 model
- MVC model
- Three-tier architecture + MVC model
Deployment method division
- Integrated structure
- vertical split structure
- Distributed structure
- Microservice Architecture
Tomcat
server
A server is a type of computer that runs faster, has a higher load, and is more expensive than a normal computer.
- The server provides computing or application services for other clients (such as PCs, smart devices, etc.) in the network.
- The server has high-speed CPU computing power, long-term reliable operation, powerful I/O external data throughput and better scalability.
The server we are talking about here is actually a web server, or an application server. It is essentially a piece of software that, by combining with hardware, can help us publish applications and allow users to access our applications through clients.
Common application server
server nickname | illustrate |
---|---|
weblogic | Implements the JavaEE specification, heavyweight server, also known as JavaEE container |
websphereAS | Implemented the JavaEE specification, heavyweight server |
JBOSSAS | Implements the JavaEE specification, heavyweight server, free |
Tomcat | It implements the jsp/servlet specification and is a lightweight server, open source and free |
Tomcat
- Tomcat is a core project in the Jakarta project group of the Apache Software Foundation, jointly developed by Apache, Sun and some other companies and individuals.
Thanks to Sun's participation and support, the latest Servlet and JSP specifications can always be reflected in Tomcat.
- Because of its advanced technology, stable performance, and free, Tomcat is deeply loved by Java enthusiasts and has been recognized by some software developers. It has become a popular Web application server.
- Tomcat official website: https://tomcat.apache.org/
- Support required for each version of Tomcat
Download and install
- Download: official website download
- Installation: Just unzip it directly
directory composition
- bin: some binary executables
- conf: The path to save the configuration file
- lib: jar package required by Tomcat during operation
- logs: log file
- temp: temporary file
- webapps: The project publishing directory (a folder represents a web application) (ROOT represents the root project)
- work: working directory
basic use
-
Startup
startup.bat
: Start the executable file under Windows: Start the executable file
startup.sh
under LinuxAfter startup, browser access: http://localhost:8080 to enter the welcome interface (Tomcat default port is 8080)
-
Stop
shutdown.bat
: Close the executable file under Windows: Close the executable file
shutdown.sh
under Linux -
Deploy the project
Create a folder in the webapps directory
Put the resources in the folder
Start tomcat and enter the correct path
common problem
-
Startup problem
The startup window flashes by: jdk environment variables are not configured
java.net.BindException: Port 8080 is occupied
-
Console garbled problem solving
conf-logging.properties
modificationjava.util.logging.ConsoleHandler.encoding = UTF-8
Tomcat defaults to UTF-8, and the CMD command window defaults to GBK. Changing UTF-8 to GBK can solve the problem of garbled characters
IDEA integrates Tomcat
- Click on Run -> Edit Configurations
- 点击Defaults -》 Tomcat Servlet -》 Local
- Click Configure - "Tomcat Home -" to select the path where tomcat is located
Installation under Linux
- Upload the compressed package to the /home path:
put d:/apache-tomcat-9.0.58.tar.gz
- Unzip the compressed package:
tar -zxvf 压缩包名
- Enter the bin directory:
cd apache-tomcat-9.0.58/bin
- Start the tomcat service:
./startup.sh
- Use browser test: browser open the corresponding Linux server ip address: 8080
Java WEB project
- Create a new project model and select Java Enterprise
to determine the JDK version and Application Server version - 右键Add Framework Support…
- Check the Web Application option
Detailed project composition
- src: store Java source code
- web: store project-related resources (html, css, js, jsp, pictures, etc.)
- WEB-INFO: Stores related configurations (web.xml, etc.)
IDEA publishes the project
- Click on Run -> Edit Configurations
- Click Tomcat Server -> Deployment
Application Context is the project access path, / represents the default path, there can only be one default path in multiple projects - Click Tomcat Server -> Server
settings associated browser
Two Update resources settings
Set JDK, port number - Start the Tomcat service
- Validation results (browser)
Publish project via war package
- Open the war package in the web path of the project:
jar -cvf myweb.war .
- Cut the prepared war package to the webapps path of tomcat
- Start the tomcat service and automatically decompress the war package
- Validation results
configuration file
Configure the default port number
main configuration fileserver.xml
<Connector>
In the label, the port attribute represents the default port number of Tomcat (8080)
The default port number of the http protocol is 80, and the default port number of Tomcat is inconsistent with it, so
you need to add the port number every time you visit
the website. It is 80, so you don't need to add the port number when accessing the website
Configure virtual directory
Role: You can publish projects in any directory
-
editserver.xmlconfig file, find the
<Host>
tag -
add the following
<Context path="/mytest" docBase="e:/test" />
path
: The virtual directory name of the access resource (representing the access path in the browser address bar)
docBase
represents the real path of the project that needs to be published
Configure virtual hosts
Role: You can specify the name of the access path
- editserver.xmlconfig file, find the
<Engine>
tag - add the following
name
: Virtual host name
appBase
: The root path where the project is saved
unpackWARs
: Whether to automatically decompress the war package
autoDeploy
: Whether to automatically publish
Context
: the same as the virtual directory
- Modify the hosts file
to bind the IP address and the corresponding domain name
file path:c:\Windows\System32\drivers\etc
HTTP protocol
Overview
- HTTP(Hyper Text Transfer Protocol):Hypertext Transfer Protocol
- HTTP protocol is based on TCP/IP protocol (secure)
- Hypertext: more powerful than normal text (plus pictures, audio, etc.)
- Transmission protocol: communication rules between client and server (handshake rules)
component
- Request: The client initiates a request to the server
- Response: The server sends a response to the client after receiving the client's request
In addition to manually initiated requests, JavaScript, CSS, images and other resources will automatically initiate requests
request for agreement
components of the request
-
Request line: Request method submission path (submission parameters) HTTP/version number
-
request header
name illustrate Accept MIME types supported by client browsers Accept-Encoding The compression encoding format supported by the client browser. The most commonly used is gzip compression Accept-Language The languages supported by the client browser. Usually zh_CN or en_US, etc. Referer Tell the server where the current request is coming from Content-Type MIME types supported by the request body Content-Length the length of the request body User-Agent Browser related information Connection The status of the connection. Keep-Alive stay connected If-Modified-Since The last modification time of the client browser cache file Cookie Session management related, very important -
Request blank line: normal newline, used to distinguish request header and request body
-
Request body: Only the POST submission method has a request body, which is used to display the submission parameters
way of request
GET
POST
Only the POST request method has a request body, and the GET request method does not have a request body
. The parameters submitted by get are in the request line, and the parameters submitted by post are in the request body.
protocol response
Components of a Response
-
Response Line: Request Mode HTTP/Version Number Status Code Status Description
Common Status Codesstatus code illustrate 200 everything is ok 302/307 Request redirection, two requests, address bar changes 304 The requested resource has not changed, use the cache 404 Requested resource not found 500 Server Error -
response header
name illustrate Location The address to request redirection, often used in conjunction with 302 and 307 Server Server related information Content-Type MIME type of the response body Content-Length the length of the response body Content-Disposition Tell the client browser to open the response body as a download Refresh Timed refresh Last-Modified The last modification time of the server resource Set-Cookie Session management related, very important Expires:-1 The cache time after the server resource is sent to the client browser Catch-Control:no-catch don't cache -
Response blank line: a normal newline, used to distinguish the response header from the response body
-
Response body: Send the resource file to the client browser for parsing
Publish resource case
Publish static resources
-
Create a Java WEB project
-
Import the resources required by the static page into the web directory of the project
-
Modify the web.xml configuration file, modify the default home page
<!--在web.xml中加入--> <welcome-file-list> <welcome-file>/路径/文件名.html</welcome-file> </welcome-file-list>
-
Deploy the project to tomcat
-
Start the tomcat service
-
Open the browser to view the page
Introduction to Servlet
- Servlet is a program running on the Java server side to receive and respond to requests from clients based on the HTTP protocol.
- If you want to achieve the function of Servlet, you can achievejavax.servlet.Servletinterface or an implementation class that inherits it.
- Core method: service(), any client request will go through this method.
Publish dynamic assets
-
Create a JavaWEB project
-
Import the resources required by the static page into the web directory of the project
-
Modify the web.xml configuration file, modify the default home page
-
Write a class under the src path of the project to implement the Servlet interface
-
Rewrite the service method and output a sentence
-
Modify the web.xml configuration file to configure servlet-related resources
<!--Servlet声明--> <servlet> <servlet-name>自定义名称</servlet-name> <servlet-class>java全类名(包名.类名)</servlet-class> </servlet> <!--Servlet映射--> <servlet-mapping> <servlet-name>和声明中的名称保持一致</servlet-name> <url-pattern>/访问路径(浏览器地址栏要输入的访问路径)</url-pattern> </servlet-mapping>
-
Deploy the project to tomcat
-
Start the tomcat service
-
Open the browser test function
Implementation process
- Access to the specified resource path through a browser
- Find the corresponding name tag through url-pattern
- Find the corresponding servlet declaration by name
- Find the corresponding Java implementation class in the declaration
- Execute the service method
Recommended reading: [Java WEB] Servlet & Annotation Development