Tomcat虚拟目录与虚拟主机的配置

目录

互联网上常用协议以及其工作端口

协议端口
http80
smtp25
pop3110
ftp23
https443

虚拟目录的映射方式

Tomcat配置虚拟目录

  在Tomcat的server.xml配置文件中的Host标签内,添加:

<Context path="/test" docBase="C:\web"/>

  其中path属性表示对外访问的路径;docBase表示本地的Web资源路径。注:配置完成后需要重启服务器。因此,在实际生产中,并不推荐使用该方法。
  如配置为:

<Context path="" docBase="C:\web"/>

  即配置了一个缺省的Web应用,默认访问的Web应用。

不重启Tomcat配置虚拟目录的方式:

  In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The context path and version will be derived from the base name of the file (the file name less the .xml extension). This file will always take precedence over any context.xml file packaged in the web application's META-INF directory.

  在Tomcat服务器的conf/[enginename]/[hostname]/目录下创建任意以.xml结尾的文件,则文件名为对外访问的Web路径,如test.xml。
  test.xml中的配置如下:

<Context docBase="C:\web"/>

  则在浏览器中访问 http://localhost:8080/test/1.html ,即访问的是服务器C盘下面的web目录下的1.html文件。
  多级目录的配置,如:test#t2#t3.xml,则在浏览器中访问 http://localhost:8080/test/t2/t3/1.html,同样访问的是服务器C盘下这个web目录的Web应用。
  如配置文件命名为 ROOT.xml(此时需要重启服务器),则配置了默认的Web应用,该应用指向的是ROOT.xml中配置的web目录下的应用。则在浏览器中可以直接访问:http://localhost:8080/1.html,访问的是服务器C盘web目录下的Web资源。

让tomcat自动映射

  tomcat服务器会自动管理webapps目录下的所有web应用,并把它映射程虚拟目录。换句话说,tomcat服务器webapps目录中的web应用,外界可以直接访问。

配置虚拟主机

  在Tomcat的server.xml配置文件中配置Host标签,如:

<Host name="www.sina.com" appBase="C:\sina">
    <Context path="/pc" docBase="C:\sina\pc">
</Host>

  同时修改本地C:\Windows\System32\drivers\etc下的HOSTS文件,加入:

192.168.1.22 www.sina.com

  其中 192.168.1.22 应相应修改为对应本机的ip地址(内网地址)。
  在浏览器中直接访问:http://www.sina.com/pc/1.html (如果端口也配置为80的话),则访问的是服务器C盘sina目录下的pc Web应用下的1.html页面。

使用Java编写最简单的服务器程序

package com.wm103.server;

import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by DreamBoy on 2017/4/25.
 */

/**
 * 最简单的Web服务器
 */
public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(9999);
        Socket socket = server.accept();

        FileInputStream in = new FileInputStream("G:\\1.html");
        OutputStream out = socket.getOutputStream();

        int len = 0;
        byte buffer[] = new byte[1024];
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();
        socket.close();
        server.close();
    }
}

  程序运行后,访问http://localhost:9999可以看到 1.html 页面的内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值