tomcat 二级域名的处理

来源:互联网 发布:软件委托开发保密协议 编辑:程序博客网 时间:2024/05/18 01:18



开始之前,首先来认识下什么是2级域名? 

一般我们注册一个域名,比如 www.javaeye.com (以下内容均以javaeye.com域名为例子,当然这只是example,该域名早已被注册了!),那么这个就是你的1级域名(顶级域名),是固定的 并且只有1个,而像 robin.javaeye.com,stephen830.javaeye.com等等就是你这个1级域名下的2级域名了。因此,每个1级域名下面可以 拥有无数个2级域名,只要你的名字符合国际域名名称定义规则。 

现在越来越多的网站提供2级域名的功能,就如JavaEye,只要您注册成为用户,您就可以当场得到一个属于自己的2级域名。可能,或许您觉得这其中很神秘,以为这个功能实现会比较复杂,其实不然,看完了本篇文章,相信您也可以轻而易举打造自己的动态2级域名效果了。 

下面就让我们一步步来看看如何实现的吧! 

第1步 您要准备一个1级域名(国际域名,国内域名都可以),并且您域名所在的dns服务器要支持域名泛解析功能,拿javaeye.com来说,泛解析就是 a.javaeye.com/b.javaeye.com/c.javaeye.com/...,所有这些都指向javaeye.com设置的同一个ip 地址,有了这个功能,域名才能够实现动态2级域名。一般域名服务商的dns服务器都有这个功能。如下图(图1): 

 (图1) 

您只需要在域名的配置中增加一条*.开头的记录,就可以实现泛解析了。配置完后,你可以用ping命令测试下,假设你的域名是 javaeye.com,你可以运行 ping a.javaeye.com,如果通的话就表示支持泛解析了。(当然,要注意有的主机服务器屏蔽ping回显!) 

第2步 准备好你的WEB应用,这里使用了tomcat,可以在tomcat的server.xml配置文件中进行配置:

Xml代码 
  1. < Host   name = "localhost"   debug = "0"   appBase = "/www/doc"   unpackWARs = "true"   autoDeploy = "true" >   
  2. < Logger   className = "org.apache.catalina.logger.FileLogger"   directory = "logs"    prefix = "www."   suffix = ".txt"  timestamp = "true" />   
  3. </ Host >   
[xml] view plaincopy
  1. <Host name="localhost" debug="0" appBase="/www/doc" unpackWARs="true" autoDeploy="true">  
  2. <Logger className="org.apache.catalina.logger.FileLogger" directory="logs"  prefix="www." suffix=".txt" timestamp="true"/>  
  3. </Host>  



其中的appBase配置自己WEB应用的位置,name="localhost",表示根目录,所有访问本主机的将自动转到appBase配置 的应用上。假设您的域名主机地址为 202.109.xx.xx,那么直接在浏览器中输入http://202.109.xx.xx 就会默认打开您在上面配置的那个web应用,这个在你的主机上有多个WEB应用的时候要小心。您在增加其他独立的WEB应用(假设还有个WEB应用是 www.xxx.com)时需要像下面这样的配置:

Xml代码 
  1. < Host   name = "www.xxx.com"   debug = "0"   appBase = "/www/doc/xxx"   unpackWARs = "true"   autoDeploy = "true" >   
  2.         < Logger   className = "org.apache.catalina.logger.FileLogger"   directory = "logs"    prefix = "www.xxx.com."  suffix = ".txt"   timestamp = "true" />   
  3. </ Host >   
[xml] view plaincopy
  1. <Host name="www.xxx.com" debug="0" appBase="/www/doc/xxx" unpackWARs="true" autoDeploy="true">  
  2.         <Logger className="org.apache.catalina.logger.FileLogger" directory="logs"  prefix="www.xxx.com." suffix=".txt" timestamp="true"/>  
  3. </Host>  



这个独立的www.xxx.com则必须使用域名来访问,不能使用网站ip地址来访问。 



第3步 为了更好的展现动态2级域名效果,使用一个frame作为WEB应用的index.html,这个index.html将作为你的WEB应用的首页面。(备注:这个index.html不是必须的,只是为了让浏览器的地址栏显的更加好看而已) 

index.html

Html代码 
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >   
  2. < html >   
  3. < head >   
  4. < title > - </ title >   
  5. < meta   http-equiv = "Content-Type"   content = "text/html; charset=utf-8" >   
  6. < head >   
  7. < frameset   framespacing = "0"   border = "0"   rows = "0,*"   frameborder = "0" >   
  8.   < frame   name = ""  noresize  scrolling = "no"   target = "_self"   src = ""   frameborder = "0"   marginheight = "0"  marginwidth = "0" >   
  9.   < frame   name = ""   scrolling = "auto"   target = "_self"   src = "index.jsp"   frameborder = "0"   marginheight = "0"  marginwidth = "0" >   
  10.   < noframes >   
  11.     < body >   
  12.     < p > 您的浏览器版本太低,需要升级后才能使用本系统! </ p >   
  13. </ body >   
  14.   </ noframes >   
  15. </ frameset >   
  16. </ html >   
[html] view plaincopy
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3. <head>  
  4. <title>-</title>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  6. <head>  
  7. <frameset framespacing="0" border="0" rows="0,*" frameborder="0">  
  8.   <frame name="" noresize scrolling="no" target="_self" src="" frameborder="0" marginheight="0" marginwidth="0">  
  9.   <frame name="" scrolling="auto" target="_self" src="index.jsp" frameborder="0" marginheight="0" marginwidth="0">  
  10.   <noframes>  
  11.     <body>  
  12.     <p>您的浏览器版本太低,需要升级后才能使用本系统!</p>  
  13. </body>  
  14.   </noframes>  
  15. </frameset>  
  16. </html>  



第4步 实现动态的2级域名。 

假设用户在网站(www.javaeye.com)上注册一个用户后,用户名比如为 test,那么只要在浏览器中输入 test.javaeye.com后,就会进行如下的处理。 

输入 test.javaeye.com , 首先会解析 test.javaeye.com 这个2级域名的ip地址,这样就像输入 http://202.109.xx.xxx/ 一样的效果 (假设 javaeye.com的ip为202.109.xx.xxx);然后就会去访问这个域名对应WEB应用的首页面,也就是上面配置的 index.html,然后会调用下面的index.jsp 

这个jsp才是真正负责进行动态2级域名解析并处理的 index.jsp

Html代码 
  1. < %@ page  contentType = "text/html;charset=UTF-8" % >   
  2. < %  
  3.     String host  =  request .getHeader("Host").trim();//获得来访者的域名信息,这里如果是2级域名,就会得到例如 test.javaeye.com, test就是注册用户名  
  4.     String[] host hosts  = host.split("[.]");  
  5.     if(hosts.length ==3){//只处理长度为3的信息  
  6.         if("javaeye".equals(hosts[1])&&"com".equals(hosts[2])){//判断是否是javaeye.com的2级域名  
  7.             String name  =  hosts [0].trim();  
  8.             if(!"www".equals(name)){//www是域名本身用的,不能用于表示2级域名,所以在用户注册的时候像www,ftp,mail,smtp,pop等关键词不要被用户注册到。  
  9.                 //在这个位置还需要用个方法来校验下name是否合法,比如name必须是网站的注册用户,将name与网站的用户表数据进行校验  
  10.                 //校验通过后,转到对应的处理页面               response.sendRedirect("self.jsp?name ="+name);//2级域名转向到指定的一个处理文件,如这里是self.jsp  
  11.                 return;  
  12.             }  
  13.         }  
  14.     }  
  15. %>   
  16. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  17. < html >   
  18. < head >   
  19. < title > -- </ title >   
  20. < meta   http-equiv = "Content-Type"   content = "text/html; charset=UTF-8" >   
  21. </ head >   
  22. < body   bgcolor = "#ffffff"   topmargin = "80px"   leftmargin = "20px"   rightmargin = "20px" >   
  23. <!--网站首页面内容-->   
  24. </ body >   
  25. </ html >   
[html] view plaincopy
  1. <%@ page contentType="text/html;charset=UTF-8"%>  
  2. <%  
  3.     String host = request.getHeader("Host").trim();//获得来访者的域名信息,这里如果是2级域名,就会得到例如 test.javaeye.com, test就是注册用户名  
  4.     String[] hosts = host.split("[.]");  
  5.     if(hosts.length==3){//只处理长度为3的信息  
  6.         if("javaeye".equals(hosts[1])&&"com".equals(hosts[2])){//判断是否是javaeye.com的2级域名  
  7.             String name = hosts[0].trim();  
  8.             if(!"www".equals(name)){//www是域名本身用的,不能用于表示2级域名,所以在用户注册的时候像www,ftp,mail,smtp,pop等关键词不要被用户注册到。  
  9.                 //在这个位置还需要用个方法来校验下name是否合法,比如name必须是网站的注册用户,将name与网站的用户表数据进行校验  
  10.                 //校验通过后,转到对应的处理页面               response.sendRedirect("self.jsp?name="+name);//2级域名转向到指定的一个处理文件,如这里是self.jsp  
  11.                 return;  
  12.             }  
  13.         }  
  14.     }  
  15. %>  
  16. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  17. <html>  
  18. <head>  
  19. <title>--</title>  
  20. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  21. </head>  
  22. <body bgcolor="#ffffff" topmargin="80px" leftmargin="20px" rightmargin="20px">  
  23. <!--网站首页面内容-->  
  24. </body>  
  25. </html>  



这样,就可以轻轻松松地实现了动态2级域名功能了。


转http://blog.csdn.net/xsxxxsxx/article/details/5691507

0 0
原创粉丝点击