JavaWeb核心技术系列教程(33)——相对路径与绝对路径


C语言自学完备手册(33篇)

Android多分辨率适配框架

JavaWeb核心技术系列教程

HTML5前端开发实战系列教程

MySQL数据库实操教程(35篇图文版)

推翻自己和过往——自定义View系列教程(10篇)

走出思维困境,踏上精进之路——Android开发进阶精华录

讲给Android程序员看的前端系列教程(40集免费视频教程+源码)


版权声明

  • 本文原创作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

在使用重定向、请求转发和超链接时均涉及到路径问题,在本节教程中我们重点讲解相对路径于绝对路径。

项目结构

JavaWeb核心技术系列教程(33)——相对路径与绝对路径_第1张图片

重定向路径问题

利用绝对路径将请求重定向至应用外

当重定向至应用外时直接使用外部地址(外部绝对路径)即可,示例如下:

//重定向至应用外部
public void testRedirect1(HttpServletRequest request, HttpServletResponse response) throws IOException {
	response.sendRedirect("http://www.ifeng.com/");
}

利用绝对路径将请求重定向至应用内

在重定向中绝对路径以/开头,该/表示Tomcat服务器地址即http://localhost:8080/

示例如下:

	// 利用绝对路径重定向至应用内
	public void testRedirect2(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//以下为错误示例
		response.sendRedirect("/SecondServlet");
		// 等效路径http://localhost:8080/SecondServlet

		//以下为正确示例
		response.sendRedirect("/PathTestProject/SecondServlet");
		// 等效路径http://localhost:8080/PathTestProject/SecondServlet

		response.sendRedirect(request.getContextPath() + "/SecondServlet");
		// 等效路径http://localhost:8080/PathTestProject/SecondServlet

		response.sendRedirect(request.getContextPath() + "/index.jsp");
		// 等效路径http://localhost:8080/PathTestProject/index.jsp
	}

利用相对路径将请求重定向至应用内

在重定向中不以/开头的路径表示相对路径。那么,究竟什么是相对路径呢?
在该示例中,假若我们要访问FirstServlet,那么其完整路径为:
http://localhost:8080/PathTestProject/FirstServlet
也就是说:FirstServlet的父路径(父目录)为:
http://localhost:8080/PathTestProject/

当我们使用相对路径进行重定向时其完整路径为:当前路径的父路径+重定向请求路径

示例如下:

	// 利用相对路径重定向至应用内
	public void testRedirect3(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 以下为错误示例
		 response.sendRedirect("PathTestProject/SecondServlet");
		// 等效路径http://localhost:8080/PathTestProject/PathTestProject/SecondServlet

		// 以下为正确示例
		 response.sendRedirect("SecondServlet");
		// 等效路径http://localhost:8080/PathTestProject/SecondServlet

		response.sendRedirect("jsps/firstPage.jsp");
		// 等效路径http://localhost:8080/PathTestProject/jsps/firstPage.jsp
	}

在此示例中,我们在FirstServlet中进行重定向,FirstServlet的父路径为http://localhost:8080/PathTestProject/,所以重定向的实际完整路径为:http://localhost:8080/PathTestProject/ + 重定向路径

请求转发路径问题

利用绝对路径将请求转发至应用内

与请求重定向不同,请求转发不可转发至应用外只可在本应用内进行转发。请求转发中绝对路径以/开头,该/表示当前应用在Tomcat服务器中的地址即http://localhost:8080/Web应用名/

示例如下:

	// 利用绝对路径请求转发至应用内
	public void testForward1(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("/jsps/firstPage.jsp");
		// 等效路径http://localhost:8080/PathTestProject/jsps/firstPage.jsp
		requestDispatcher.forward(request, response);

		 RequestDispatcher requestDispatcher = request.getRequestDispatcher("/other.jsp");
		// 等效路径http://localhost:8080/PathTestProject/other.jsp
		 requestDispatcher.forward(request, response);
	}

利用相对路径将请求转发至应用内

利用相对路径进行请求转发原理同利用相对路径进行重定向类似;故,在此不再赘述。

示例如下:

	// 利用相对路径请求转发至应用内
	public void testForward2(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("SecondServlet");
		// 等效路径http://localhost:8080/PathTestProject/SecondServlet
		requestDispatcher.forward(request, response);
		
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsps/firstPage.jsp");
		// 等效路径http://localhost:8080/PathTestProject/jsps/firstPage.jsp
		requestDispatcher.forward(request, response);
	}

超链接路径问题

利用绝对路径链接至应用外

示例如下:

<a href="http://www.ifeng.com/">链接至http://www.ifeng.com/</a>

利用绝对路径链接至应用内

在超链接中以/开头的路径为绝对路径,该/表示Tomcat服务器地址即http://localhost:8080/

示例如下:

<a href="/PathTestProject/FirstServlet">链接至应用内Servleta>
<br />
<br />
<a href="${pageContext.request.contextPath}/FirstServlet">链接至应用内Servleta>
<br />
<br />

利用相对路径链接至应用内

在超链接中不以/开头的路径为相对路径,原理同利用相对路径进行重定向类似;故,在此不再赘述。

示例如下:

<a href="SecondServlet">链接至应用内Servleta>

示例源码

index.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>index</title>
	</head>
	<body>
		<h2 style="color: red;">本文作者:谷哥的小弟</h2>
		<h2 style="color: red;">博客地址:http://blog.csdn.net/lfdfhl</h2>
		
		<h3>利用绝对路径链接至应用内</h3>
		<a href="/PathTestProject/FirstServlet">链接至应用内Servlet</a>
		<br />
		<br />
		<a href="${pageContext.request.contextPath}/FirstServlet">链接至应用内Servlet</a>
		<br />
		<br />
		
		<h3>利用相对路径链接至应用内</h3>
		<a href="SecondServlet">链接至应用内Servlet</a>
		<br />
		<br />
		
		<h3>利用绝对路径链接至应用外</h3>
		<a href="http://www.ifeng.com/">链接至http://www.ifeng.com/</a>
    </body>
</html>

JavaWeb核心技术系列教程(33)——相对路径与绝对路径_第2张图片

other.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>other</title>
	</head>
	<body>
		<h2>This is other jsp page</h2>
	</body>
</html>

FirstServlet

package cn.com;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 本文作者:谷哥的小弟 
 * 博客地址:http://blog.csdn.net/lfdfhl
 */
public class FirstServlet extends HttpServlet {

	private static final long serialVersionUID = -4577198103079096245L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		testRedirect1(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	//重定向至应用外
	public void testRedirect1(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.sendRedirect("http://www.ifeng.com/");
	}
	
	// 利用绝对路径重定向至应用内
	public void testRedirect2(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 以下为错误示例
		// response.sendRedirect("/SecondServlet");
		// 等效路径http://localhost:8080/SecondServlet

		// 以下为正确示例
		// response.sendRedirect("/PathTestProject/SecondServlet");
		// 等效路径http://localhost:8080/PathTestProject/SecondServlet

		// response.sendRedirect(request.getContextPath() + "/SecondServlet");
		// 等效路径http://localhost:8080/PathTestProject/SecondServlet

		// response.sendRedirect(request.getContextPath() + "/index.jsp");
		// 等效路径http://localhost:8080/PathTestProject/index.jsp
	}
	
	// 利用相对路径重定向至应用内
	public void testRedirect3(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 以下为错误示例
		// response.sendRedirect("PathTestProject/SecondServlet");
		// 等效路径http://localhost:8080/PathTestProject/PathTestProject/SecondServlet

		// 以下为正确示例
		// response.sendRedirect("SecondServlet");
		// 等效路径http://localhost:8080/PathTestProject/SecondServlet

		response.sendRedirect("jsps/firstPage.jsp");
		// 等效路径http://localhost:8080/PathTestProject/jsps/firstPage.jsp
	}
	
	// 利用绝对路径请求转发至应用内
	public void testForward1(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("/jsps/firstPage.jsp");
		// 等效路径http://localhost:8080/PathTestProject/jsps/firstPage.jsp
		requestDispatcher.forward(request, response);

		// RequestDispatcher requestDispatcher = request.getRequestDispatcher("/other.jsp");
		// 等效路径http://localhost:8080/PathTestProject/other.jsp
		// requestDispatcher.forward(request, response);
	}
	
	// 利用相对路径请求转发至应用内
	public void testForward2(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("SecondServlet");
		// 等效路径http://localhost:8080/PathTestProject/SecondServlet
		requestDispatcher.forward(request, response);
		
		// RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsps/firstPage.jsp");
		// 等效路径http://localhost:8080/PathTestProject/jsps/firstPage.jsp
		// requestDispatcher.forward(request, response);
	}
}

SecondServlet

package cn.com;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SecondServlet extends HttpServlet{

	private static final long serialVersionUID = -4958934382759516442L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().write("This is the second servlet");
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}

firstPage.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>First Page</title>
	</head>
	<body>
		<h1>This is the first page</h1>
	</body>
</html>

secondPage.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>Second Page</title>
	</head>
	<body>
		<h1>This is the second page</h1>
	</body>
</html>

web.xml


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>PathTestProjectdisplay-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>
  <servlet>
    <servlet-name>FirstServletservlet-name>
    <servlet-class>cn.com.FirstServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>FirstServletservlet-name>
    <url-pattern>/FirstServleturl-pattern>
  servlet-mapping>
  <servlet>
    <servlet-name>SecondServletservlet-name>
    <servlet-class>cn.com.SecondServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>SecondServletservlet-name>
    <url-pattern>/SecondServleturl-pattern>
  servlet-mapping>
web-app>

你可能感兴趣的:(JavaWeb核心技术系列教程)