项目需求:所有jsp页必须通过Action转发,不能直接在地址栏链接jsp或<a href="aaa.jsp"></a>
我的应用如下:
1.LoginFilter
public class LoginFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest hRequest = (HttpServletRequest)request;
HttpServletResponse hResponse = (HttpServletResponse)response;
String uri = hRequest.getRequestURI();
System.out.println("uri======"+uri);
TBussinessInfo info = (TBussinessInfo) hRequest.getSession().getAttribute("user");
if( "/b2cPlatform/jsp/phone/login/login.jsp".equals(uri) || "/b2cPlatform/".equals(uri)
|| "/b2cPlatform/jsp/phone/login/trunToLogin.jsp".equals(uri) )
{
chain.doFilter(request, response);
}
else
{
hResponse.sendRedirect(hRequest.getContextPath());//不允许直接访问jsp页,除非login.jsp等页
}
}
public void destroy() {}
public void init(FilterConfig arg0) throws ServletException {}
}
2.web.xml
<!-- 登录Filter -->
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.hanpeng.b2c.phone.priv.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
二、网上其他资料
<%=request.getContextPath()%>是解决相对路径的问题,可返回站点的根路径。
<a href="<%=request.getContextPath()%>/XXX.jsp"> //这样获得的是绝对路径
<a href="XXX.jsp"> //这样获得的是相对路径
<a href="<%=request.getContextPath()%>/XXXX.jsp"> 能够更有效的防治连接的失效。
request.getContextPath()得到的是项目的名字,如果项目为根目录,则得到一个"",即空的字条串,
如果项目为dzjc, <%=request.getContextPath()% >/ 将得到dzjc/,服务器端的路径则会自动加上,
<a href="XXX.jsp"> 应该就是指当前路径下的这个xxx.jsp页面,有时候也可以在head里设置html:base
来解决路径的问题,不过用的最多的还是request.getContextPath。
用EL来表示相同功能的EL为:${pageContext.request.getContextPath()}
request.getScheme();
返回的协议名称,默认是http
request.getServerName()
返回的是你浏览器中显示的主机名
getServerPort()
获取服务器端口号
如果想得到工程文件的实际物理路径,可通过:<%=request.getRealPath("/")%>,这样页面就会输出:d:/web
现在request.getRealPath("") 这个方法已经不推荐使用了
可以使用
ServletContext.getRealPath(java.lang.String) instead. request.getSession().getServletContext().getRealPath() 得到工程文件的实际物理路径,也就是绝对地址
简便用法:
String path=request.getContextPath();//path = /oa
request.setAttribute("path", path);
在页面中应用(EL) ${path}/images/xx.jpg(绝对定位:oa/images/xx.jpg)
可以解决复杂的相对路径定位失败的问题。
分享到:
相关推荐
request.getcontextPath()详解
java中request.getcontextPath()的意思.doc
response.sendRedirect(request.getContextPath()+"/admin/login.jsp"); } public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //...
Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别
基于用户兴趣标签的新闻推荐系统(毕业设计) 注意事项 界面的修改,均已index.jsp修改为主,在进行子界面的修改,保持统一性!...注意事项,jsp页面中的img标签添加获取项目名路径 ( request.getContextPath() )
注意事项,jsp页面中的img标签添加获取项目名路径 ( request.getContextPath() ) 原理图 架构-服务端 架构-客户端 主页-查询思路 推荐策略(后台) 推荐显示(前台) 推荐通知原理(Android) 爬虫原理 总的...
本文实例讲述了JSP实现简单的用户登录并...String path = request.getContextPath(); String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”; %>
**request.getContextPath()**返回虚拟路径: /hansom **request.getScheme()**返回使用的协议:http **request.getServerName()**返回所在服务器的名字:localhost **request.getServerPort()**返回IP端口:8080...
if (session.getAttribute("person") == null) ... response.sendRedirect(request.getContextPath() + "/login.jsp"); }else{ request.getRequestDispatcher("fall.jsp").forward(request, response); }
var so = new SWFObject("<%=request.getContextPath()%>/js/player/CuPlayerMiniV4.swf","CuPlayerV4","600","410","9","#000000"); so.addParam("allowfullscreen","true"); so.addParam("allowscriptaccess",...
String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD H
String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML> <html> <head> ...
=request.getContextPath()%>是解决相对路径的问题,可返回站点的根路径。 <a>/XXX.jsp”> //这样获得的是绝对路径 <a> //这样获得的是相对路径 <a>/XXXX.jsp”> 能够更有效的防治连接的失效。 request....
本文实例讲述了jsp获取url路径的方法。分享给大家供大家参考,具体如下: 如果你请求的URL是 http://localhost:8080/demo/Index.jsp request.getScheme() //输出:http request.getServerName() //输出: ...
dwr开发包,常规客户端...<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/TestService.js'> <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'> ...
NULL 博文链接:https://javachikuang.iteye.com/blog/262264
String getContextPath() ---web应用的名称 String getQueryString() ---- get提交url地址后的参数字符串 username=zhangsan&password=123 注意:request获得客户机(客户端)的一些信息 request.getRemoteAddr() -...
My97DatePicker日期控件使用引入js脚本目录,注意必须用<%=request.getContextPath() %>来获取项目根目录+脚本目录的方式,否则日期控件会无效!!! <script type="text/javascript" src="<%=request....
String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 的详解
用户管理系统。语言JAVA public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, ... response.sendRedirect(request.getContextPath()+"/manager.jsp"); }