JavaWeb学习笔记(三)--Tomcat服务器(二)

一、Web应用的组成结构

开发web应用时,不同类型的文件有严格的存放规则,否则不仅可能会使web应用无法访问,还会导致web服务器启动报错。

web.xml文件是整个web应用中最重要的配置文件,必须放置在WEB-INF目录中。在开发web应用时,但凡涉及到对web应用中的web资源的配置,通通是在web.xml文件中进行配置。例如:

  • 某个web资源配置为网站首页
  • 将servlet程序映射到某个url低智商
  • 为web应用配置监听器
  • 为web应用配置过滤器

web.xml配置举例:将某个web资源配置为首页

按照web应用的组成结构创建JavaWebDemo应用程序,放到webapps下面,让web服务器自动映射虚拟目录,补充web.xml(可以参考webapps下的examples中的,赋值xml头部和尾部),把首页改成自己html。

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">


    <welcome-file-list>
        <welcome-file>home.html</welcome-file>
    </welcome-file-list>

</web-app>

运行结果:

二、配置虚拟主机

1. 修改server.xml,增加一个新的主机名site:

<Host name="site"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>

2. 修改C:\Windows\System32\drivers\etc\hosts

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#    127.0.0.1       localhost
#    ::1             localhost
127.0.0.1       site   #这个是新增的

运行结果:

猜你喜欢

转载自www.cnblogs.com/songchj-bear/p/10322440.html
今日推荐