JSP(2)—绝对路径与相对路径、配置Servlet与Servlet注解
一、绝对路径和相对路径
①开发时建议使用据对路径,使用绝对路径肯定没有问题,但是用相对路径可能会有问题。
在由Servlet转发到JSP页面时,此时在浏览器地址栏显示Sevvlet路径,若JSP页面的超链接还是相对与该JSP页面的地址,
则可能会出现路径混乱的问题。
例如: A.jsp———>B.jsp———>C.jsp
(子目录)/Demo7_RelativePathAndAbsolutePath/A.jsp
:<a href="B.jsp">To B Page</a>
(子目录)/Demo7_RelativePathAndAbsolutePath/B.jsp
:<a href="../C.jsp">To C Page</a>
(根目录)/C.jsp
:<a href="Demo7_RelativePathAndAbsolutePath/A.jsp">To A Page</a>
一般开发过程中页面之间不会进行跳转,都使用Servlet进行中转之后在选择跳转的页面
(子目录)
<a href="<%=request.getContextPath()%>/Demo7_RelativePathAndAbsolutePath/B.jsp">To B Page</a>
(子目录)<a href="<%=request.getContextPath() %>/C.jsp">To C Page</a>
(根目录)<a href="<%=request.getContextPath()%>/pathServlet">To A Page</a>
Servlet:
@WebServlet("/pathServlet")
public class PathServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<String> cities = Arrays.asList("北京","上海","广州");
request.setAttribute("cities", cities);
//通过转发的方式响应C.jsp
request.getRequestDispatcher("/Demo7_RelativePathAndAbsolutePath/A.jsp").
forward(request, response);
}
}
②绝对路径:相对与当前web应用的根路径的路径。即任何路径都要带上ContextPath
例如http://localhost:8080/Demo7_Cookie_Session/C.jsp,C.jsp就可以说是绝对路径
③如何使用绝对路径:
若/代表的是站点的根目录,在其前面加上contextPath()即可,而contextPath可以由request或
application的getContextPath()方法获取。
例如:
<a href="<%=request.getContextPath()%>/Demo7_RelativePathAndAbsolutePath/B.jsp">
To B Page</a>
<a href="<%=request.getContextPath() %>/C.jsp">To C Page</a>
<a href="<%=request.getContextPath()%>/pathServlet">To A Page</a>
request.getRequestDispatcher("/Demo7_RelativePathAndAbsolutePath/A.jsp").
forward(request, response);
2.JavaWeb开发过程中/代表什么
①当前web应用的根路径:http://localhost:8080/Demo7_Cookie_Session/容器
其中/交由Servlet来处理
1).request请求转发时:
request.getRequestDispatcher("/Demo7_RelativePathAndAbsolutePath/A.jsp").
forward(request, response);
2).web.xml文件中Servlet-mapping:
<servlet-mapping>
<serlet-name>step2Servlet</servlet-name>
<url-pattern>/step2Servlet</url-pattern>
</servlet-mapping>
3).各种定制标签
②web站点的根路径:http://localhost:8080/
其中交由浏览器处理
1).超链接
<a href="/pathServlet">To A Page</a>
2).表单中的action
<form action="/index.jsp" method="post"></form>
3).做请求重定向的时候
response.sendRedirect("/step-2.jsp");
二、Servlet注解和Servlet配置文件
在Servlet3.0之前使用Servlet都需要在web.xml中配置Servlet,Servlet3.0之后可以使用@WebServlet注解的方式进行配置,使用方便。所以不管是在超链接或者form表单的action中都可以使用这两种方式,但是还是action中使用多一点,一般不会页面之间互相访问,而且action中同时也使用绝对路径,以免造成混乱。
方式一在web.xml中配置:
<servlet>
<display-name>step1Servlet</display-name>
<servlet-name>step1Servlet</servlet-name>
<servlet-class>com.shopCart.Servelt.Step1Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>step1Servlet</servlet-name>
<url-pattern>/step1Servlet</url-pattern>
</servlet-mapping>
方式二:使用@WebServlet注解
@WebServlet("/pathServlet")
public class PathServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<String> cities = Arrays.asList("北京","上海","广州");
request.setAttribute("cities", cities);
//通过转发的方式响应C.jsp
request.getRequestDispatcher("/Demo7_RelativePathAndAbsolutePath/A.jsp").
forward(request, response);
}
}
注意:这两种方式不能同时使用,否则会出现异常:
Server Tomcat v7.0 Server at localhost failed to start.
Caused by: java.lang.IllegalArgumentException: The servlets named [step1Servlet] and [com.shopCart.Servelt.Step1Servlet] are both mapped to the url-pattern [/step1Servlet] which is not permitted
最新文章
- jQuery1.6源码分析系列
- ipv4理论知识2-分类编址、ip分类、网络标识、主机标识、地址类、地址块
- IOS网络第二天 - 04-黑酷-GDataXML 解析
- storyboard中的三种传值
- win10使用小技巧以及常见问题处理方案
- Linux下cp -rf总是提示覆盖的解决办法
- 【BZOJ1968】【AHoi2005】COMMON约数研究
- pepperflash
- 【汉诺塔问题】UVa 10795 - A Different Task
- C#连接sqlserver数据库
- selenium之表格的定位
- angular.toJson()
- json11阅读
- forEach、for、$.each()跳出循环比较
- 廖雪峰Java2面向对象编程-2数据封装-1方法重载
- js中获取一个对象里面的方法和属性的javascript
- YAML文件简介
- Java入门到精通——调错篇之Eclipse No Java virtual machine was found after searching the following locations
- poj3308 Paratroopers 最大流 最小点权覆盖
- 关于C++的宏:WIN32和DEBUG
热门文章
- 1.RN环境搭建,创建项目,使用夜神模拟调试
- DirectoryEntry配置IIS7出现ADSI Error:未知错误(0x80005000)
- python全栈开发day54-mysql库操作、表操作、数据类型、完整性约束
- net core体系-网络数据采集(AngleSharp)-1初探
- Python 私有属性
- window下面的守护进程,redis守护进程,RunHiddenConsole免费下载
- 068 mapWithState函数的讲解
- 在 Wiki 标记中添加无序列表
- JAVA基础中的注意点(一)
- M &#215; N Puzzle POJ - 2893(奇数码)