javaweb Note 1: Tomcat server

Software System Architecture

1.1 Common software system architecture

CS

  1. C/S structure is client/server (Client/Server), such as QQ;

  2. It is necessary to write server-side programs and client-side programs, for example, what we installed is the QQ client-side program;

l Disadvantages: When updating the software, both the client and the server need to be updated at the same time, which is troublesome;

l Advantages: better security.

BS

  1. The B/S structure is the browser/server (Browser/Server);

  2. Advantages: only need to write server-side programs;

l Disadvantages: poor security

2 WEB resources

2.1 Web resource introduction

Static resources :

A static resource can be opened directly by a browser. For a js file, select IE browser to open it in the opening method. If the browser can directly open it without any problems, it means it is a static resource. HTML files, css files, js files, etc. are all static resources.

Dynamic resource :

Generally, the dynamic resource requested by the client first submits the request to the web container, and the web container connects to the database. After the database processes the data, it passes the content to the web server, and the web server returns it to the client for analysis and rendering. . For example, jsp files, servlets, php, ASP, etc. are all dynamic resources.

insert image description here

2.3 Accessing web resources

Open a browser and enter the URL:

Protocol name://domain name:port/path, for example: http://www.itcast.cn:80/index.html

3 Web servers

The role of the web server is to receive the client's request and respond to the client.

For JavaWeb programs, JSP/Servlet containers are also required. The basic function of JSP/Servlet containers is to convert dynamic resources into static resources. Of course, JSP/Servlet containers are not limited to these functions, and we will learn bit by bit later.

What we need to use is a Web server and a JSP/Servlet container, usually in one. Here's the Java Web Server:

  • Tomcat (Apache): currently the most widely used Java Web server;
  • JBoss (Redhat Red Hat): supports JavaEE, widely used; EJB container
  • GlassFish (Oracle): Oracle develops JavaWeb server, which is not widely used;
  • Resin (Caucho): supports JavaEE and is widely used;
  • Weblogic (Oracle): Money is required! Support JavaEE, suitable for large projects;
  • Websphere (IBM): Money! Support JavaEE, suitable for large projects;

Tomcat server

1 Overview

Tomcat is an open source and free jsp server, which is a lightweight application server. It can realize the loading of JavaWeb programs, and is a necessary environment for configuring JSP (Java Server Page) and JAVA systems.

2. Installation and configuration

1. Download: http://tomcat.apache.org/
2. Set environment variables:
Before starting Tomcat, we must configure environment variables:

  • JAVA_HOME: JAVA_HOME must be configured first, because Tomcat needs to use JDK to start
  • CATALANA_HOME: If it is the installation version, you also need to configure this variable. This variable is used to specify the installation path of Tomcat, for example: F:\apache-tomcat-7.0.42.
  • Start: Enter the %CATALANA_HOME%\bin directory, find startup.bat, and double-click it;
  • Shutdown: Enter the %CATALANA_HOME%\bin directory, find shutdown.bat, and double-click it;

startup.bat will call catalina.bat, and catalina.bat will call setclasspath.bat, setclasspath.bat will use the JAVA_HOME environment variable, so we must configure JAVA_HOME correctly before starting Tomcat

2.3 Enter the Tomcat home page

Access: http://localhost:8080

2.4 Configure the port number

Open the %CATALANA_HOME%\conf\server.xml file:

insert image description here
The default port number of http is 80, which means that port 80 is used when the port number is not given in the URL. Of course, you can also modify it to other port numbers.

After changing the port number to 80, you only need to enter: http://localhost in the browser to visit the Tomcat home page.

2.5Tomcat directory structure

insert image 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 rights configuration file: tomcat-users.xml
  • Default configuration file for all web projects: web.xml
  • lib: Dependent libraries, jar packages that need to be used in tomcat and web projects
  • logs: log files, localhost_access_log..txt tomcat records user access information, star indicates time. For example: localhost_access_log.2016-02-28.txt
  • temp: Temporary file directory, the contents of the folder can be deleted arbitrarily.
  • webapps: By default, the directory where the WEB project is stored is published.
  • work: the working directory of tomcat processing JSP.

Web application (emphasis)

Static website :

  1. Create a directory under the webapps directory (the name must not contain Chinese and spaces), this directory is called the project directory;
  2. Create an html file in the project directory;

Dynamic site :

  • Create a project directory under the webpass directory;

  • Create the following content in the project directory:
    WEB-INF directory;
    create a web.xml file in the WEB-INF directory
    Create static or dynamic pages

2.6 Directory Structure of Web Application

insert image description here

  • mail: application directory, mail is the name of the application;
  • index.html: Application resources. There can be multiple resources under the application, such as css, js, html, jsp, etc., and the resources can also be placed in a folder, for example: hello\html\index.html, then the access URL is: http://localhost: 8080/hello/html/index.html;
  • WEB-INF: The name of this directory must be uppercase, and the things in this directory cannot be directly accessed through the browser, which means that the things placed here are safe;
  • web.xml: The deployment descriptor file of the application, in which the application can be configured, such as configuring the home page of the application:
  • classes: the directory where class files are stored;
  • lib: the directory where the jar package is stored;

2.7 Configure external applications (understand)

It turned out that our project was placed under webapps, but now I put it outside, and I hope tomcat can find it!

You can also put the application outside of Tomcat, which is an external application. For example, we cut the mail application written above from the webapps directory to the C drive, that is, C:/mail. Now the web application mail is no longer in Tomcat. At this time, we need to configure the location of the external application in Tomcat. There are two configuration methods:

  1. conf/server.xml: Open the server.xml file, find the element, and add elements in it, the code is as follows:

server.xml
insert image description here

  • path: specifies the name of the current application;
  • docBase: specifies the physical location of the application;
  • Browser access path: http://localhost:8080/itcast_hello/index.html.
  1. conf/catalana/localhost: Create the itcast_hello.xml file in this directory, write elements in this file, the code is as follows:

insert image description here

  • File name: specify the name of the current application;
  • docBase: specifies the physical location of the application;
  • Browser access path: http://localhost:8080/itcast_hello/index.html.

IDEA deployment Tomcat

When using the original JSP development, idea needs to configure tomcat to run the web program. After reinstalling the system, many configurations have been forgotten. Here it is re-recorded.

The first step: install tomcat (jdk must be installed in advance, so I won't go into details)

Installing tomcat is very simple, go directly to the official website and decompress the required zip version to the local. (A lot of open source software is so convenient. After deleting, you only need to delete the file directory. It would be great if Microsoft software is so convenient)
insert image description here

After decompression, modify the port of the server.xml configuration file to prevent port conflicts, and then start it. It can be started through the console or by installing the service.

Because it is for the idea, you only need to install it in the idea and start it dynamically.

Step 2: The default idea is that there is no web server and needs to be set manually.

Open the File-Settings menu
insert image description here
and select Application Servers, click the Add button on the right to select "Tomcat Server" (idea supports many kinds of webservers that can be easily integrated with external servers)
select Application Servers, click the Add button on the right to select "Tomcat Server" (idea It supports many kinds of webservers and can be easily integrated with external servers)
insert image description here
The Tomcat Home directory can be directly selected to decompress the directory
insert image description here
, so tomcat is configured, how to use it?

You need to configure RUN/DEBUG configer before running the program.
insert image description here
insert image description here
Note: The above tomcat port step needs to be consistent with the default port of tomcat, which can be configured flexibly. This is similar to the dynamic port setting in VS IIS EXPRESS debugging. It is very convenient to modify back and forth without worrying about port conflicts. .

Next step:

If you want to run a website, you need to configure deployment, which is similar to the website publishing settings in ASP.NET. Why do you need to set it? Tomcat runs outside the idea. Only the website directory published to Tomcat can run web programs. This is automatically published to the Tomcat directory. In, and then send the tomcat startup command, the automatic startup of the web program is realized

insert image description here
insert image description here
参考资料:
1.https://www.cnblogs.com/ginb/p/7217817.html
2.https://www.cnblogs.com/justdoitba/category/1086662.html
3.https://blog.csdn.net/bell_love/article/details/105667638

Guess you like

Origin blog.csdn.net/weixin_42838061/article/details/121128704