Day 12 绝对路径相对路径
来源:互联网 发布:tomcat修改端口号 编辑:程序博客网 时间:2024/06/04 23:29
一.绝对路径相对路径
1.开发时一般用绝对路径,写绝对路径可定不会出现问题,写相对路径有可能会出现问题
再由Servlet转发到JSP页面是,此时浏览器上显示的是Servlet的路径,而JSP页面的超链接还是相对于该JSP页面的地址,则可能会出现路径混乱的问题
2.编写绝对路径可以避免上述问题
1)绝对路径:相对于当前WEB应用的根路径的路径(即所有路径都得加contextPath)
如/代表的是站点的根目录则在前面加contextPath就好(而contextPath可以由request或application的getContextPath()方法来获取)
例如:<%a href="<=request.getContextPath()%/TestServlet">to b</a>
response.sendRedirect(request.getContextPath()+"/path/c.jsp")
http://localhost:8080/contextPath(当前WEB应用的上下文路径)/a.jsp ---是绝对路径
http://localhost:8080/a.jsp --------不是
>超链接:<a href="TestServlet">to b</a>
>表达式中的action:<form action="/login.jsp">
>请求重定向的时候:request.getRequestDispatcher("/path/b.jsp").forward(request, response)
3.例:
相对路径:
a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h4>aaa</h4><a href="TestServlet">to b</a></body></html>TestServlet.java
package com.atguigu.javaweb;import java.io.IOException;import java.util.Arrays;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class TServlet */@WebServlet("/TServlet")public class TServlet extends HttpServlet {private static final long serialVersionUID = 1L;/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubString name="aaaccbb";request.setAttribute("name", name);request.getRequestDispatcher("/path/b.jsp").forward(request, response);}}
path/b.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h4>bbb</h4><br>cities:<%= request.getAttribute("name") %><a href="c.jsp">to c </a></body> </html>c.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h4>ccc</h4><a href="../a.jsp">to a </a></body></html>
当从a到b后再到c就出问题了
改为绝对路径:
a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h4>aaa</h4><a href="<%= request.getContextPath() %>/TestServlet">to b</a></body></html>
b.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h4>bbb</h4><br>cities:<%= request.getAttribute("name") %><a href="<%= request.getContextPath() %>/path/c.jsp">to c </a></body> </html>
c.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h4>ccc</h4><a href="<%= request.getContextPath() %>/a.jsp">to a </a></body></html>
- Day 12 绝对路径相对路径
- 绝对路径与相对路径 15.9.12
- 相对路径※绝对路径
- 相对路径、绝对路径
- Java-相对路径/绝对路径
- 相对路径和绝对路径
- 相对路径,绝对路径问题
- 相对路径※绝对路径
- 绝对路径和相对路径
- 相对路径※绝对路径
- 相对路径绝对路径
- 相对路径与绝对路径
- 相对路径和绝对路径
- 绝对路径与相对路径
- 相对路径和绝对路径
- 相对路径与绝对路径
- 绝对路径 ? 相对路径?
- 相对路径,绝对路径
- HDU - 1560----DNA sequence
- Android Studio配置SVN
- redis window密码配置文件启动
- QCamera使用QGraphicsVideoItem输出图像
- 出现次数最多的整数
- Day 12 绝对路径相对路径
- Power of Cryptography POJ - 2109
- python技巧(12)
- Lua学习笔记——基础篇
- HashMap在非线程安全下的死循环
- Lua学习笔记——进阶
- Hdu-5921 Binary Indexed Tree(数位DP)
- Lua学习笔记——C语言与Lua的交互
- 这是开始