今天利用java的反射机制把HttpServletRequest的各种路径打印了一下,以前老记错,其实这种东西用到时再查也行,没必要老记着:
部分代码如下,因为是直接在项目的web容器里面弄的,所以没有写request的获取代码,唉,各种struts框架,搞得现在连获取这种底层的东西都要费好大劲,算了,暂时不去管了,其实以前也弄过好多次不过都忘了。废话不说了,直接上代码:
Method[] meth = HttpServletRequest.class.getDeclaredMethods();
for (int i = 0; i < meth.length; i++) {
Object obj = null;
String methname=meth[i].getName();
try {
if(meth[i].getParameterTypes().length>0){
}
else{
obj = meth[i].invoke(request);
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String value = obj==null?"":obj.toString();
System.out.println("request方法名称--"+methname+":"+value);
}
结果:(稍微做了处理)
request方法名称--getRequestURI--对应值:/new_mss/idx_IndexView_indexViewPortlet.action
request方法名称--getRequestURL--对应值:http://localhost:8080/new_mss/idx_IndexView_indexViewPortlet.action
request方法名称--getServletPath--对应值:/idx_IndexView_indexViewPortlet.action
request方法名称--getContextPath--对应值:/new_mss
也还是有一点成就感的。汗!