迷彩 潜伏 隐蔽 伪装
分类: 架构设计与优化
2014-07-15 10:29:24
原文地址:用urlrewrite做静态化和二级域名 作者:hiyachen
在做动态页面静态化时用到urlrewrite、现在准备做网店二级域名的需求。需要再次用到urlrewrite,当然需要整合使用。后续会总结:
一级域名:
二级域名:
这里aboy其实会Mapping到用户ID,根据用户ID来展示不用用户的Blog,javaabc是通过Ruby实现的, 那Java如何实现这个Mapping呢?
需求1.2:
我们查看具体的哪一篇文章是通过 http://aboy.mytest.com/blog/785171 来访问的,这个URL是如何传参数的呢?
思路2:
A. 通过过滤器获得二级域名.(eg. aboy)
B. 对URL进行重写Rewrite.
实现3:
3.1 创建一个URLFilter过滤器,对所有的请求进行过滤
Java代码 复制代码 收藏代码
1.package com.aboy.web;
2.
3.import java.io.FileInputStream;
4.import java.io.FileNotFoundException;
5.import java.io.IOException;
6.import java.util.ArrayList;
7.import java.util.List;
8.import java.util.regex.Matcher;
9.import java.util.regex.Pattern;
10.
11.import javax.servlet.Filter;
12.import javax.servlet.FilterChain;
13.import javax.servlet.FilterConfig;
14.import javax.servlet.ServletException;
15.import javax.servlet.ServletRequest;
16.import javax.servlet.ServletResponse;
17.import javax.servlet.http.HttpServletRequest;
18.
19.import com.aboy.web.URLRewriter.URLMapping;
20.import com.thoughtworks.xstream.XStream;
21.import com.thoughtworks.xstream.io.xml.DomDriver;
22.
23./**
24. * Servlet Filter implementation class URLFilter
25. */
26.public class URLFilter implements Filter {
27.
28. List
29.
30. public URLFilter() {
31. // TODO Auto-generated constructor stub
32. }
33.
34. /**
35. * @see Filter#init(FilterConfig)
36. */
37. public void init(FilterConfig fConfig) throws ServletException {
38.
39. /**
40. * 初始化
41. */
42. String configPath=fConfig.getInitParameter("configPath");
43. String realConfigPath=fConfig.getServletContext().getRealPath(configPath);
44. FileInputStream input;
45. byte buff[]=null;
46. try {
47. input = new FileInputStream(realConfigPath);
48. buff=new byte[input.available()];
49. input.read(buff);
50. input.close();
51.
52. } catch (FileNotFoundException e) {
53. // TODO Auto-generated catch block
54. e.printStackTrace();
55. } catch (IOException e) {
56. // TODO Auto-generated catch block
57. e.printStackTrace();
58. }
59. /**
60. * 通过开源XStream来解析XML,并转化成Java对象
61. */
62. XStream xstream = new XStream(new DomDriver());
63. xstream.alias("url", URLMapping.class);
64. xstream.alias("URLList", ArrayList.class);
65. urlPatternList=(List)xstream.fromXML(new String(buff));
66. }
67. /**
68. * @see Filter#destroy()
69. */
70. public void destroy() {
71. // TODO Auto-generated method stub
72. }
73.
74. /**
75. * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
76. */
77. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
78.
79. HttpServletRequest httpServletRequest = (HttpServletRequest) request;
80. /**
81. * 获得服务器名称,如:aboy.mytest.com/
82. */
83. String serverName=request.getServerName();
84. int endIndex=serverName.indexOf(".");
85. /**
86. * 获得二级域名
87. * 二级域名一般是一个ID, 如:aboy
88. */
89. String secondDomainName=serverName.substring(0, endIndex);
90. request.setAttribute("userID", secondDomainName);
91. if(secondDomainName.equalsIgnoreCase("www")){
92. httpServletRequest.getRequestDispatcher( "/index.html")
93. .forward(request, response);
94. return;
95.
96. }else{
97. String realPath=this.getRealPath(httpServletRequest.getRequestURI());
98. httpServletRequest.getRequestDispatcher("/"+realPath)
99. .forward(request, response);
100. return;
101. }
102. }
103.
104. /**
105. * 将URI转换成真正的请求URL
106. * e.g aboy.mytest.com/blog/785171 ->
107. * aboy.mytest.com/dispatcher.do?action=displayBlog&blogId=785171
108. * @param URI
109. * @return
110. */
111. private String getRealPath(String URI){
112. for(URLMapping ){
113. Pattern pattern=Pattern.compile(url.getPattern());
114. Matcher matcher=pattern.matcher(URI);
115. if(matcher.find()){
116. String reqPara=url.getReqParameter();
117. for(int i=1;i<=matcher.groupCount();i++){
118. reqPara=reqPara.replace("${"+i+"}", matcher.group(i));
119. }
120. return reqPara;
121.
122. }
123. }
124. return URI;
125.
126. }
127.
128.
129.}
3.2 创建URLMapping类
Java代码 复制代码 收藏代码
1.package com.aboy.web.URLRewriter;
2.
3.public class URLMapping {
4.
5. String pattern;
6. String reqParameter;
7. public String getPattern() {
8. return pattern;
9. }
10. public void setPattern(String pattern) {
11. this.pattern = pattern;
12. }
13. public String getReqParameter() {
14. return reqParameter;
15. }
16. public void setReqParameter(String reqParameter) {
17. this.reqParameter = reqParameter;
18. }
19.
20.}
3.3 在Web.xml中配置过滤器,对所有请求都进行处理
Xml代码 复制代码 收藏代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
3.4 在/WEB-INF下创建URLRewriter.xml
Xml代码 复制代码 收藏代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
附言: 如果要在本地测试二级域名,需要在hosts文件中配置如下参数(C:\WINDOWS\system32\drivers\etc\hosts)
127.0.0.1 aboy.mytest.com