Java Web Learning (i)

Source: Internet
Author: User

One, Web resources

Introduction to "1"Web Resources

1. Static resources: Web static resources, such as HTML, can be resolved through a browser.

2. Dynamic resources: He is not able to show directly, it needs to be converted to static resource HTML before being parsed by the browser. For example: Jsp/servlet.

"2" The difference between static resources and dynamic resources

The picture above shows the difference between static and dynamic resources, how do we access Web resources? Usually implemented through the browser, open the browser, enter the URL:

Protocol name://domain: Port/path, for example: http://www.baidu.com

Ii. introduction of Tomcat

installation and configuration of "1" Tomcat

1. Install Tomcat to make sure that your computer has a JDK installed. We only explain the installation of the press plate here. Unzip the version of Tomcat to a non-Chinese, no space in the path, it is recommended that the path is not too deep, because we need to frequently enter the Tomcat installation directory. Example: F:\apache-tomcat-7.0.42

2. Before starting Tomcat, we have to configure the environment variables:

(1) Java_home: The Java_home must be configured first because the Tomcat boot requires the use of the JDK

(2) Catalana_home: In the case of the installation version, you also need to configure this variable, which is used to specify the installation path for Tomcat,

Example: F:\apache-tomcat-7.0.42

(3) Start, enter the%catalana_home%\bin directory, click Startup.bat; Close, go to%catalana_home%\bin directory, click Shutdown.bat

After you start Tomcat, enter http://localhost:8080 on the browser to jump to the Tomcat home page, indicating that the installation was successful.

directory structure for "2" Tomcat

(1) Bin: This directory is a binary executable file, if the installation version, then this directory will have two EXE files: Tomcat6.exe, Tomcat6w.exe, the former is launched in the console tomcat, the latter is the popup ugi window to start Tomcat, if it is the decompression version, then there will be Startup.bat and shutdown.bat files, Startup.bat used to start Tomcat, but need to first configure Java _home environment variable to start, Shutdawn.bat to stop Tomcat.

(2) Conf: This is a very, very important directory with four of the most important files in this directory:

    • Server.xml: Configures the entire server information. For example, modify the port number, add a virtual host, and so on, this file is described in detail below
    • Tomcat-users.xml: Stores the Tomcat user's file, where it holds the Tomcat user name and password, as well as the user's role information. You can add a tomcat user by pressing the comment information in the file, and then you can go to the Tomcat Manager page in the Tomcat homepage
    • Web. xml: Deployment descriptor file, which registers many MIME types, which are document types. These MIME types are document types that are documented between the client and the server, such as when a user requests an HTML page, and the server also tells the client that the browser responds with a text/html type of document, which is a MIME type. The client browser knows how to handle it through this MIME type. This HTML file is, of course, displayed in the browser. However, if the server responds to an EXE file, then the browser will not be able to display it, but should pop up the download window. MIME is used to describe what type of document the content is!
    • Context.xml: Unified configuration for all applications, usually we do not configure it.

(3) Lib:tomcat class library, which is a large heap of jar files. If you need to add a tomcat-dependent jar file, you can put it in this directory, of course, you can also put the application dependent jar files in this directory, this directory of jar all items can be shared, However, it is not possible to share the jar package in this directory when your application is placed under other tomcat, so it is recommended that only the jar packages that Tomcat needs are placed in this directory.

(4) Logs: This directory is a log file that logs tomcat startup and shutdown information, and if there is an error starting Tomcat, the exception is also recorded in the log file.

(5) Temp: A temporary file for tomcat that can be deleted after the Tomcat is stopped.

(6) WebApps: A directory for Web projects, where each folder is a project, and if the directory already exists, it is a tomcat-brought project, such as: Docs, examples, Host-manager, manager, and Root. Where Root is a special item, when the URL that we enter in the address bar of the browser does not give the project directory, the default is the root project. For example: Http://localhost:8080/examples, enter the sample project. Where examples is the project name, which is the name of the folder.

(7) Work: Files generated at run time, eventually running files are here. Generated by the project in WebApps! You can delete the contents of this directory and run it again to generate the work directory again. When a client user accesses a JSP file, Tomcat generates a Java file from the JSP and then compiles the Java file to generate the class file, and the resulting Java and class files will be stored in this directory.

"3"Tomcat as a Web server application

1. Create a static app

    • Create a Hello directory under the WebApps directory of the Tomcat installation directory
    • Create index.html under Webapps\hello\, the contents of the HTML file can be written in accordance with the format requirements
    • Launch Tomcat, open the browser to access http://localhost:8080/hello/index.html

2. Create a dynamic app

    • Create a hello1 directory under WebApps
    • Create a web-inf directory under webapps\hello1\
    • Create the Web. Xml and Classes and LIB directories under webapps\hello1\web-inf\
    • Create a index.html under webapps\hello1\
    • Launch Tomcat, open the browser to access http://localhost:8080/hello/index.html

The directory structure is as follows:

    • Java-web: Application directory. Java-web is the name of the app
    • INDEX.JSP: Application resources. The application can have a number of resources, such as CSS, JS, HTML, JSP, etc., can also put resources into folders, for example: hello\jsp\index.jsp at this time the access URL is: http://localhost:8080/hello/html/index.html
    • Web-inf: This directory name must be uppercase, the contents of this directory can not be accessed directly through the browser, that is, the things put here is safe
    • Web. XML: The application's deployment descriptor file, which can be configured in the file. We'll explain in more detail how to configure the deployment profile later

"Quad" configuration external Applications

The deployed project was originally placed in the WebApps directory of the Tomcat installation directory, and now we don't put it in that directory, we put it under the other directory, and hopefully Tomcat can find it.

For example, we cut the Hello app written above from the WebApps directory to the D disk, which is D:/hello. Now that the web app for Hello is no longer in Tomcat, we need to configure the location of the external app in Tomcat in a total of two ways:

    • Locate Conf/server.xml: Open the Server.xml file, locate the <Host> element, add the <Context> element in it, and the code is as follows:

(1) Path: Specifies the name of the current app, where the path name can be either hello or any other legitimate app name.

(2) DocBase: Specifies the physical location of the application, that is, the path stored on the disk.

(3) The URL visited is: http://localhost:8080/my/index.html.

    • Conf/catalana/localhost: Create the My.xml file in this directory, write the <Context> element in the file, and the code is as follows:

(1) file name: Specifies the name of the current app.

(2) DocBase: Specifies the physical location of the application.

(3) The URL visited is: http://localhost:8080/my/index.html.

Java Web Learning (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.