JDK版本:17.0.2
Tomcat版本:10.0.17
建议删除Tomcat重新安装一个全新的,以免发生未知的错误。
1.创建域文件
在Tomcat所在文件夹里创建名为webprog的文件夹,在webprog中再创建名为ROOT的文件夹,再在ROOT里面创建webtest.txt,文件内容为webprogtest-problem 1。
2.修改server.xml
打开Tomcat文件夹-conf-server.xml,拉到文件末尾。在原有之后添加选中的部分。
unpackWARs="true" autoDeploy="true">
在AAA的位置填上你的webprog的绝对路径,其他的不变,保存。
3.修改系统hosts
这一步主要是为了在浏览器中能访问成功,不影响telnet访问,不然会出现404,因为这个虚拟主机是在局域网上的,浏览器去公网上找不到位置,需要手动告诉电脑网址对应的ip地址。
在C:WindowsSystem32driversetc下,找到hosts文件用记事本打开,在最后一行加上:127.0.0.1 www.webprog.org
如果没有hosts文件,在桌面创建host.txt文件复制以下内容:
# 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 localhost 127.0.0.1 www.webprog.org
保存txt文件,并把文件移动到上述位置。
4.修改web.xml
这一步也是为了浏览器能够访问,不影响telnet,将webtest.txt设置成欢迎页,不然浏览器会返回403拒绝访问。
打开conf中的web.xml,拉到最后,添加选中的那一行,保存退出。
5.telnet访问
打开cmd,键入:startup.bat启动Tomcat。
新建一个txt文件,输入如下内容:
注意空格,host不区分大小写。host表示访问虚拟主机的哪个域,不同的域文件不互通。
重新打开一个cmd,键入:telnet localhost 8080或者 telnet www.webprog.org 8080。两者没有区别,因为在hosts文件里已经告诉电脑localhost和www.webprog.org都指向127.0.0.1,此ip地址代表的就是本机,所以直接写ip地址也是可以的,8080则代表进入到本机的Tomcat应用,即虚拟主机。
回车后是一个纯黑界面,键入:ctrl+],出现如下界面:
直接回车后又是一个纯黑界面,复制刚才创建的txt文档中的两句话,在纯黑界面右键,然后回车(可能要两次),注意要快,不然会遗失主机。而且不能修改,输入错误即使修改对也会报错。
成功!
如果想通过浏览器访问可以输入:www.webprog.org:8080。



