您的位置:首页 > 运维架构 > Tomcat

[WEB]服务器访问原理与Tomcat服务器安装配置

2016-05-03 10:22 645 查看
1.域名与本机HOST文件:本地hosts文件(C:\WINDOWS\system32\drivers\etc\hosts)管理域名与ip地址对应关系,可以手动修改,把sina.com映射成本机ip都可以,也可以用于屏蔽网站和阻止自动访站软件。我电脑上的localhost域名默认对应于ip地址(应该是一个局域网动态或虚拟ip):127.0.0.1

2.ip与端口号:服务端的服务器软件或程序监听某端口,客户端软件或程序通过服务端ip+端口号访问服务端该程序,服务端同时开启的服务端软件不允许占用同一个端口以避免冲突。

3.数据传输:特殊的网络流对象,服务端通过客户端套接字获得并操纵,输入输出的目标位置(文件或终端)完全由程序通过IO流设定,一般服务器是接收客户端请求的URL并切割出文件路径,用来初始化IO流对象,确定访问目标,提供相应传输服务。

一个简单的小例子:

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

public class WebServer {

public static void main(String args[]) throws IOException {
ServerSocket server = new ServerSocket(9999);

Socket sock = server.accept();// 侦听并接受到此套接字的连接

FileInputStream fi = new FileInputStream(
"C:\\Documents and Settings\\Administrator\\My Documents\\1.html");
OutputStream out = sock.getOutputStream();// 从请求套接字处获得流

// 写
int len = 0;
byte buffer[] = new byte[1024];
while ((len = fi.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
fi.close();
out.close();
sock.close();
server.close();
}
}


(注:我一开始用Eclipse编写,创建了一个包,导致执行时找不到此类的一个小问题(classpath的问题,我没配置环境变量,而java虚拟机到classpath去找Class文件),因为没加上包名,这里暂时去掉包,因为我的服务程序要在命令行执行,用浏览器(客户端)访问)

开启服务端程序,accept方法阻塞式等待(Socket请求):

D:\java>javac WebServer.java

D:\java>java WebServer

开启浏览器访问对应ip+端口------------------>此时相当于我的机器既充当服务器(命令行服务程序)又充当客户端(浏览器访问):

输入URL:http://localhost:9999/

浏览器显示C:\\Documents and Settings\\Administrator\\My Documents\\1.html中的内容:

ergesrgg


Tomcat服务器:Tomcat是用java写成的程序,在执行时会去环境变量中寻找java虚拟机所在位置,所以要启动Tomcat服务器需要事先设置JAVA_HOME环境变量,否则不能正常开启(一闪而过)。classpath不需要配置,默认当前目录。

Catalina_home环境变量:决定启动的Tomcat服务器的位置,就是决定打开startup访问的是哪台Tomcat服务器,一般不要配置。

端口的问题:实际开发中默认8080端口,上线运营时用80端口,浏览器会根据相应协议(http)默认访问80端口。

当在配置文件中设置成了80端口后,打开服务器,用浏览器访问时不用再指定端口,直接访问成功。注意同一台机器上同时运行的程序不能占用同一端口,比如再打开Apache服务器就不能正常工作,因为都是80端口。需要关闭一个占用同一端口的程序(如果是后台服务,需要在Windows服务中关闭,否则Windows会自动将其打开)

常用协议端口:http-->80 smtp-->25 pop3-->110 ftp-->23 https-->443

设置web应用的虚拟目录映射:一般采用Tomcat给出的第三种方式------>在其conf\Catalina\localhost目录下建立.xml文件,文件名(a,或a#b#c形式)作为虚拟目录路径或级联路径,内容中设置:

<Context docBase="d:\news"/>

则虚拟目录a/b/c映射为应用所在真实目录d:\news

设置缺省目录映射(需要重启服务器):在同样的目录下建立ROOT.xml文件,内容相同,则可直接输入应用名(缺省路径)访问

设置映射的另一种方式:在server.xml配置文件中添加:

<Context path="/a" docBase="d:\news"/>------------------------------------------------------>别忽视path的路径名前边带着/啊!!!

重启服务器,则a/映射到d:\news目录

设置缺省目录映射,添加:

<Context path="" docBase="d:\news"/>

重启服务器,直接输入应用名访问

服务器自动虚拟目录映射:Tomcat服务器会自动管理webapps目录下的所有web应用,并映射成虚拟目录。换句话说,webapps目录下的应用,外界可直接访问(只加上webapps目录中应用的子目录名)

web应用组织结构和web.xml配置应用首页:

组织结构:

|-----webapps

|--------mail

|------------html,js,jsp

|------------WEB-INF

|---------------------classes:java应用程序所在目录

|---------------------lib:java应用所需jar包所在目录

|---------------------web.xml:web应用配置文件

在web.xml中配置应用首页,参考Tomcat的web.xml配置文件中的头尾,配置内容如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at
 http://www.apache.org/licenses/LICENSE-2.0 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

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

</web-app>


即设置mail应用目录下的1.html为应用首页,通过http://localhost:8080/mail直接访问

再修改Tomcat服务器缺省的虚拟目录映射,在server.xml中加入:

<Context path="" docBase="D:\Program Files\apache-tomcat-7.0.69\webapps\mail"/>
重启服务器

则缺省访问mail应用目录,那么通过
http://localhost:8080/1.html
或者直接访问
http://localhost:8080/
均显示mail应用下的1.html内容

如果再修改为80端口,则可直接访问:
http://localhost
配置虚拟主机:主机名和ip地址存在映射关系,通过浏览器访问主机名,浏览器先在本机hosts文件中查找对应ip地址,再去公网的DNS服务器上查找。知道了这个原理,我们就可以自己配置和映射主机名,让浏览器找我们需要映射的主机名。这里映射为本机ip.

1.更改服务器配置:

在server.xml文件中仿造localhost主机名配置添加:

<Host name="www.googleBack.com" appBase="C:\google"
unpackWARs="true" autoDeploy="true">

<Context path="" docBase="D:\googleBack\news"/>
</Host>


这里设置了一个主机名,一个自动虚拟目录映射位置(C:\google),一个其他位置的虚拟目录映射(缺省映射)

在D:\googleBack\news应用下配置应用组织结构,

再修改本机hosts文件的主机名映射,添加:

127.0.0.1       www.googleBack.com


保存配置,重启服务器,访问(将服务器改为80端口):
http://www.googleback.com/1.html
成功。

在应用组织结构的WEB_INF目录下的web.xml文件中配置应用首页:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at
 http://www.apache.org/licenses/LICENSE-2.0 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<welcome-file-list>
<welcome-file>2.html</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>


保存配置,重启服务器,访问:
http://www.googleback.com/
成功。

输入的主机名的两个作用:1.到本地或公网DNS查找ip地址2.通过web请求告诉要访问的服务器,要访问哪台主机(因为同一个服务器可以有多个主机名)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: