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

来源:互联网 发布:宁波淘宝托管公司排名 编辑:程序博客网 时间:2024/06/08 08:10

目录

  • 目录
  • 互联网上常用协议以及其工作端口
  • 虚拟目录的映射方式
    • Tomcat配置虚拟目录
    • 不重启Tomcat配置虚拟目录的方式
    • 让tomcat自动映射
  • 配置虚拟主机
  • 使用Java编写最简单的服务器程序

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

协议 端口 http 80 smtp 25 pop3 110 ftp 23 https 443

虚拟目录的映射方式

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 页面的内容。

0 0
原创粉丝点击