`
wuhui
  • 浏览: 50925 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

java配合tomcat实现动态2级域名效果

    博客分类:
  • java
阅读更多
开始之前,首先来认识下什么是2级域名?

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

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

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

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

(图1)

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

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

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

<Host name="localhost" debug="0" appBase="/www/doc" unpackWARs="true" autoDeploy="true">
<Logger className="org.apache.catalina.logger.FileLogger" directory="logs"  prefix="www." suffix=".txt" timestamp="true"/>
</Host>

其中的appBase配置自己WEB应用的位置,name="localhost",表示根目录,所有访问本主机的将自动转到appBase配置的应用上。假设您的域名主机地址为 202.109.xx.xx,那么直接在浏览器中输入http://202.109.xx.xx 就会默认打开您在上面配置的那个web应用,这个在你的主机上有多个WEB应用的时候要小心。您在增加其他独立的WEB应用(假设还有个WEB应用是 www.xxx.com)时需要像下面这样的配置:
Xml代码
<Host name="www.xxx.com" debug="0" appBase="/www/doc/xxx" unpackWARs="true" autoDeploy="true"> 
        <Logger className="org.apache.catalina.logger.FileLogger" directory="logs"  prefix="www.xxx.com." suffix=".txt" timestamp="true"/> 
</Host> 

<Host name="www.xxx.com" debug="0" appBase="/www/doc/xxx" unpackWARs="true" autoDeploy="true">
        <Logger className="org.apache.catalina.logger.FileLogger" directory="logs"  prefix="www.xxx.com." suffix=".txt" timestamp="true"/>
</Host>


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



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

index.html

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>-</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
<frameset framespacing="0" border="0" rows="0,*" frameborder="0">
  <frame name="" noresize scrolling="no" target="_self" src="" frameborder="0" marginheight="0" marginwidth="0">
  <frame name="" scrolling="auto" target="_self" src="index.jsp" frameborder="0" marginheight="0" marginwidth="0">
  <noframes>
    <body>
    <p>您的浏览器版本太低,需要升级后才能使用本系统!</p>
</body>
  </noframes>
</frameset>
</html>

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

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

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

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

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



转载 http://stephen830.iteye.com/blog/277522
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics