In Java Web Development, you will install Tomcat first. What is Tomcat? It is a Web server, also known as a Web container, we put the written code (JSP, HTML) into it, and then launch it, it can be accessed from outside (the browser address bar entered localhost for access). You may remember Wamp, just started to do front-end development, do not have a server to do, we will download Wamp to install, and then put our written HTML code in its WWW directory, with the desktop icon to launch it, the browser address bar input localhost, You can access the HTML code that we wrote. The principle of Tomcat and the principle of wamp is the same, put into the content, start the service, you can use the browser to access. We can write a simple code to try it out and of course we'll install Tomcat first.
Install Tomcat, very simple, to Tomcat's official website http://tomcat.apache.org/Download zip compression package on it, version, you can choose casually, I chose Tomcat 8.5.23 here. After the download is complete, unzip it to the place where the usual software is placed, and I put it on the D drive. Note that Tomcat's boot relies on the JVM, so be sure to configure the Java development environment first.
It says, it's a Web server, so how do we start it? In the apache-tomcat-8.523 folder (hereinafter referred to as the Tomcat folder) there is a bin directory, the bin directory has a startup.bat
Double-click on it to see a black Tomcat command window with a cursor flashing at the bottom indicating a successful start. At this point in the browser address bar, enter localhost:8080, you can see the following screen, also indicates that the server started successfully.
It also says that we can put the code we wrote into Tomcat and then access it. In Wamp, we put the HTML file in the WWW directory, and in Tomcat we put the file in the WebApps directory, and there is a WebApps directory in the Tomcat folder.
In the WebApps directory, create a new MyHome folder, and then in the MyHome folder, create a new index.jsp file with the following contents:
<!DOCTYPE HTML><HTMLLang= "en"> <Head> <MetaCharSet= "Utf-8"> <Metaname= "Viewport"content= "Width=device-width, initial-scale=1, Shrink-to-fit=no"> <Metaname= "Theme-color"content= "#000000"> <title>React App</title> </Head> <Body> <DivID= "root">Hello World</Div> </Body></HTML>
In addition to the index.jsp file, create a new Web-inf folder, which is a secure directory for the Java Web app that the browser cannot access, and only the server can access the content. It doesn't matter if you don't understand it here. There is a examples folder in the WebApps folder,
Open it, you can see a web-inf folder, copy it to the MyHome folder. Here with Startup.bat to open the server, in the browser address bar input http://localhost:8080/myHome/index.jsp, you can see Hello world
This small example also illustrates that Tomcat is a Web server under Java development.
Here are the three main files in the Web-inf folder, which will be used later
1,web.xml, the entire project of the deployment document.
2,classes: Our entire project compiles a. class file
3,lib: We use the entire project for external files, such as jar packages.
Now we are developing with eclipse, and since Eclipse does not have a built-in Tomcat server, we need to configure Tomcat into eclipse first. On the Eclipse menu, window--->preferences, select Server (or input server) in the left option--->runtime environments, there is an add on the right, click, Pop Up the following dialog box,
Here, we select Tomcat v8.5, and then tick the create a new local server below, click Next,
In Tomcat installation directory, select the installation directory of our Tomcat, in the JRE, select the JDK that we installed, click Finish, then click Apply and Close, configuration is complete. The server directory appears in the left panel of Eclipse. At this point we configure the server in the server panel to quickly start the server.
Under the Server panel are prompted, we directly click on the underline, pop up the dialog box,
Select Tomcat v8.5 Server, then click Finish, then the server panel appears. When we select the server, click the Green Start button in the upper right corner to start the server.
Now let's use eclipse to implement the above MyHome project. File--->new--->other---> enter Web---> select Dynamic Web Project such as:
Click Next, enter Project Name:myhome, and click Finish. This is where the eclipse left panel has more of our new project.
At this time we see a webcontent folder, it has a web-inf below, when we think of the content in WebApps, then we in the WebApps myhome built index.jsp, Web-inf. This means that in the WebContent directory, we can build index.jsp files. We'll copy the contents of the WebApps myhome directly to WebContent.
Getting Started with Java Web Development---tomcat/servlet/jsp