Controller:服务器路径
// 获得项目的路径
ServletContext sc = request.getSession().getServletContext();
// 上传位置
String path = sc.getRealPath("/img") + "/"; // 设定文件保存的目录
jsp
一:获取路径:访问路径
jsp中访问后台需要url,一般不会把url写死,会使用代码来获取基本路径,以便以后的修改。
1.request.getContextPath(); :/webName
2.request.getScheme() :协议名字
3.request.getServerName():服务器名字
4.request.getServerPort():端口号
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5.request.getServletPath(),返回当前页面所在目录下全名称:/course/index.jsp;
6.request.getRequestURL(),返回IE地址栏地址:http://localhost:8080/WebName/course/index.jsp;
7.request.getRequestURI() ,返回包含工程名的当前页面全路径:/webName/course/index.jsp
二:路径处理
1.parent.formateUrl("controller/edit.html","_self")
2.<c:url value="/testController/test.html" />
本文主要介绍在Controller和JSP中如何获取服务器路径。通过request对象的方法,如getContextPath()、getScheme()、getServerName()等,可以获取到包括访问路径、协议、服务器名、端口等信息。同时讲解了处理路径的不同方式,如获取Servlet路径、请求URL和URI。
1302

被折叠的 条评论
为什么被折叠?



