`
还有也许
  • 浏览: 164369 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

学习urlrewritefilter的几点注意

    博客分类:
  • wap
阅读更多

其实我觉得urlrewritefilter做的就是将浏览器中一个动态的地址变成静态的,比如我在浏览器中输入

http://localhost:8080/wapDemo/13浏览器如何去执行以前那个完整的写法呢

http://localhost:8080/wapDemo/infoList.do?corp_account=13   再者http://localhost:8080/wapDemo/13/24 他就会执行

http://localhost:8080/wapDemo/infoDisp.do?corp_account=13&corp_product=24 

大家首先会想到写个过滤器在web.xml中配置一下,这当然是可以的,而其我已经写好了类似的处理

比如请看我的AuthFilter.java


 

package com.running.wap.support;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AuthFilter extends HttpServlet implements Filter{

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterchain) throws IOException, ServletException {
		
        boolean   b,b2;  
        String account=null;
        String account2=null;
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		//获得浏览器中输入的url地址
		String url = req.getRequestURL().toString();
		
		//得到请求uri,比如url为http://localhost:8080/wapDemo/13,则uri为
		//http://localhost:8080/ 以后的东西,此时为wapDemo/13
		String uri=req.getRequestURI();
		//如果url不是以.do结尾
		if (!url.endsWith(".do")){
			//接着就是对该uri这个字符串进行处理的过程,大体思路是获得第一个/的位置,如果有两个就获得两个/的位置
			//接着看你截获的字符串是不是数字类型的,然后获得那一个或两个数值,将其当做参数传到相应的位置
			int index=uri.lastIndexOf("/");
			int index2=uri.lastIndexOf("/",index-1);
			
			if(index>-1&&index2>-1){
			    account=uri.substring(index + 1);
			    account2=uri.substring(index2+1,index);
			     b  = Pattern.compile("\\d+").matcher(account).matches();  
			     b2 = Pattern.compile("\\d+").matcher(account2).matches();
			   if(b&&b2){
					 int corp_account,corp_product;
					 corp_product = Integer.parseInt(account);
					    corp_account = Integer.parseInt(account2);
					    resp.sendRedirect("../infoDisp.do?corp_account="+corp_account+"&corp_product="+corp_product);
					 }
			   else if(b&&b2==false){
					int corp_account;
					corp_account = Integer.parseInt(account);
				    resp.sendRedirect("infoList.do?corp_account="+corp_account);
				}
			}
		 
		if(url.equals("http://192.168.1.5:8080/wapDemo/")){
				resp.sendRedirect("index.do");
		    }		
	
		}else{
			filterchain.doFilter(request, response);

		}
		
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}

 

但因为现在流行比较火的处理该问题的jar包urlrewritefilter的诞生,我们就不用写这些了,直接在urlrewrite.xml中配置就行了,看我的相应的配置

  

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
        "http://tuckey.org/res/dtds/urlrewrite2.6.dtd">
<urlrewrite>
    <rule>
        <from>^/(\d+)$</from>
        <to type="redirect">%{context-path}/infoList.do?corp_account=$1</to> 
    </rule>
    <rule>
        <from>^/(\d+)/(\d+)$</from>
        <to type="redirect">%{context-path}/infoDisp.do?corp_account=$1&amp;corp_product=$2</to>
    </rule>
    <!--  
    <outbound-rule>
        <from>index.jsp</from>
        <to>/</to>
    </outbound-rule>
    -->
</urlrewrite>

 

就可以了,不用再伤脑筋写你自己的过滤器了。

还有就是web.xml中

   

<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

 

配置的作用,就是说当用户在浏览器中输入http://Ip:port/项目名/      之后会去找你的index.jsp页面,所以可以在你的

index.jsp中这样写

   

<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%
response.sendRedirect("index.do");
%>

 

相当于在浏览器中输入http://IP:port/项目名/index.do 一个效果,是不是很巧妙呢。

  

 

2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics