javaweb中路径的书写总结

        在写javaweb项目的时候,总会遇到路径书写的问题,现在将其作个总结。

        在javaweb中需要书写路径的地方主要有这四大类:

        客服端路径

                超链接

                表单

                重定向

        服务器端路径

                转发

                包含

        资源获取路径

                servletContext获取资源

                ClassLoader获取资源

                Class获取资源

        <url-pattern>路径

现分别作介绍

其构建的javaweb如下:

1客服端路径

A超链接

 

<%@ 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>页面A</title>
</head>
<body>
	<!-- 
		超链接和表当都有三种书写路径的方式
			1,绝对地址	
			2,以"/"开头的相对地址
			3,不以"/"开头的相对地址 
	-->
	
	<!-- 1.绝对地址	 -->
		<!-- 完整的URL -->
	<a href="http://localhost:8080/javaee/jsp/b.jsp">这是绝对地址超链接</a><br/>
	
	<!-- 2.以"/"开头的相对地址	 -->
		<!-- /代表了整个web项目,即:http://localhost:8080/ -->
	<a href="/javaee/jsp/b.jsp">这是以"/"开头的相对地址超链接</a><br/>
	
	<!-- 3.不以"/"开头的相对地址 	 -->
		<!-- 
			不以/开头,则相对于当前资源的路径
			当前资源的路径为:http://localhost:8080/javaee/jsp/
			而b.jsp也在此路径下
			所以直接书写b.jsp
		 -->
	<a href="b.jsp">这是不以"/"开头的相对地址超链接</a><br/>
</body>
</html>

 

B表单

 

 

<%@ 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>
	<!-- 所有的表单都是提交到b.jsp -->
	<!-- 
		表当提交路径有三种书写方式 
			1,绝对地址	
			2,以"/"开头的相对地址
			3,不以"/"开头的相对地址 
	-->
	<form action="http://localhost:8080/javaee/jsp/b.jsp" methoe="get">
		username:<input type="text" name="username" value="">
		<input type="submit" value="提交---绝对地址	">
	</form>
	<!-- 
		以/开头的相对地址
		
		此时的/代表整个web项目,即:http://localhost:8080/
	-->
	<form action="/javaee/jsp/b.jsp" methoe="get">
		username:<input type="text" name="username" value="">
		<input type="submit" value="提交---以/开头的相对地址">
	</form>
	
	<form action="b.jsp" methoe="get">
		username:<input type="text" name="username" value="">
		<input type="submit" value="提交---不以/开头的相对地址 ">
	</form>
	
	<!-- 表单提交到Servlet -->
	<!-- 
		表单提交到Servlet有三种书写方式
		1,绝对路径
		2,以"/"开头的相对地址
		3,不以"/"开头的相对地址 
	-->
	<!-- 1.绝对地址	 -->
		<!-- 完整的URL -->
	<form action="http://localhost:8080/javaee/PathServlet" methoe="get">
		username:<input type="text" name="username" value="">
		<input type="submit" value="表单提交到Servlet---绝对地址">
	</form>
	
	<!-- 2.以/开头的相对地址	 -->
		<!-- 此时的/代表整个web项目,即:http://localhost:8080/  -->
	<form action="/javaee/PathServlet" methoe="get">
		username:<input type="text" name="username" value="">
		<input type="submit" value="表单提交到Servlet---以/开头的相对地址">
	</form>
	<!-- 3.不以/开头的相对地址    -->
		<!--	
			不以/开头的相对路径是相对于当前资源的路径
			此时form.jsp的地址为:http://localhost:8080/javaee/jsp/form.jsp
			所以当前资源路径为:http://localhost:8080/javaee/jsp
			而要提交的Servlet的路径为Http://localhost:8080/javaee/PathServlet
			所以路径为当前路径的上一级路径下
			即路径为:../PathServlet
			注:.代表当前路径
			   ..代表当前路径的上一级路径
		-->
	<form action="../PathServlet" methoe="get">
		username:<input type="text" name="username" value="">
		<input type="submit" value="表单提交到Servlet---不以/开头的相对地址">
	</form>
</body>
</html>

 

C重定向

 

 

package cn.ccnu.path;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
 * 重定向有三种路径书写方式
 * 		1.绝对路径
 * 		2.以"/"开头的相对路径
 * 		3.不以"/"开头的相对路径
 */
public class RedirectServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.sendRedirect("http://localhost:8080/javaee/jsp/b.jsp");
		/*
		 * 2.以"/"开头的相对路径
		 * 		此时,/代表整个web工程的路径,即http://localhost:8080/
		 */
//		response.sendRedirect("/javaee/jsp/b.jsp");
		/*
		 * 3.不以"/"开头的相对路径
		 * 		此时是相对于当前资源的相对路径
		 * 		当前资源路径为:http://localhost:8080/javaee/RedirectServlet
		 * 		即表示:RedirectServlet在路径http://localhost:8080/javaee之下
		 * 		而b.jsp在http://localhost:8080/javaee/jsp/b.jsp
		 * 		所以最终地址写为:jsp/b.jsp
		 */
//		response.sendRedirect("jsp/b.jsp");
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

 

2服务器端路径

 

A请求转发

 

package cn.ccnu.path;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
 * 服务器端的路径不能是绝对路径,只能是相对路径,也分为以/开头和不以/开头两种
 * 		1.以"/"开头的相对路径
 * 		2.不以"/"开头的相对路径
 */
public class DispatcherServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 1.以"/"开头的相对路径
		 * 		此时,/代表当前web项目,即:http://localhost:8080/javaee
		 */
//		request.getRequestDispatcher("/jsp/b.jsp").forward(request, response);
		/*
		 * 2.不以"/"开头的相对路径
		 * 		相对于当前资源的相对路径
		 * 	此时,当前资源的路径为:http://localhost:8080/javaee/DispatcherServlet
		 *  所以要转发去的资源的路径以:http://localhost:8080/javaee开头
		 */
		request.getRequestDispatcher("jsp/b.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

 

B请求包含

 

 

package cn.ccnu.path;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
 * 请求包含不能书写绝对地址,只能书写相对地址
 * 		1.以"/"开头的相对路径
 * 		2.不以"/"开头的相对路径
 * 
 */
public class IncludeServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 1.以"/"开头的相对路径
		 * 		此时,/代表当前web项目,即:http://localhost:8080/javaee
		 */
//		request.getRequestDispatcher("/jsp/b.jsp").include(request, response);
		/*
		 * 2.不以"/"开头的相对路径
		 * 		相对于当前资源的相对路径
		 * 	此时,当前资源的路径为:http://localhost:8080/javaee/IncludeServlet
		 *  所以要转发去的资源的路径以:http://localhost:8080/javaee开头
		 */
		request.getRequestDispatcher("jsp/b.jsp").include(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

 

3资源获取路径

 

AServletContext获取资源

 

package cn.ccnu.path;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
 * ServletContext获取资源必须是相对路径,不能是绝对路径,但不管是以/开头,还是不以/开头,
 * 都是相对于当前资源的相对路径
 * 
 */
public class ServletContextServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String path1 = this.getServletContext().getRealPath("/a.properties");
		String path2 = this.getServletContext().getRealPath("a.properties");
		System.out.println(path1);
		System.out.println(path2);
		//输出的地址一样
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

 

BClassLoader获取资源

 

 

package cn.ccnu.classloaderpath;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
 * ClassLoader类加载器不能通过绝对地址来加载资源,只能通过相对地址来加载资源
 * 但相对地址不管前面加不加/都是相当于类路径的相对地址
 * 
 */
public class ClassLoaderServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 加了/,其地址是相对于类路径的相对地址
		 */
//		InputStream in = this.getClass().getClassLoader().getResourceAsStream("/cn/ccnu/classloaderpath/c.properties");
//		Properties prop = new Properties();
//		prop.load(in);
//		System.out.println(prop.getProperty("url"));
		
		/*
		 * 不加/,其地址是相对于类路径的相对地址
		 */
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("cn/ccnu/classloaderpath/c.properties");
		Properties prop = new Properties();
		prop.load(in);
		System.out.println(prop.getProperty("url"));
		/*
		 * 总结:不能使用绝对地址,而只能只用相对地址
		 * 且不管加不加/的相对地址,都是相对于类路径的相对地址
		 * 
		 */
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

 

CClass获取资源

 

 

package cn.ccnu.classpath;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
 * Class读取资源不能是绝对路径,只能是相对路径,又分为以/开头或者是不以/开头
 * 		1.以/开头的相对路径
 * 		2.不以/开头的相对路径
 */
public class ClassServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 1.以/开头的相对路径
		 * 		此时的/代表类路径,即:/javaee/WEB-INF/classes
		 */
//		InputStream in = ClassServlet.class.getResourceAsStream("/cn/ccnu/classpath/b.properties");
//		Properties porp = new Properties();
//		porp.load(in);
//		System.out.println(porp.getProperty("url"));
		/*
		 * 2.不以/开头的相对路径
		 * 		此时相对的是:类ClassServlet.class的路径,即:\javaee\WEB-INF\classes\cn\ccnu\classpath
		 * 		即:/javaee/WEB-INF/classes/cn/ccnu/classpath
		 */
		InputStream in = ClassServlet.class.getResourceAsStream("b.properties");
		Properties porp = new Properties();
		porp.load(in);
		System.out.println(porp.getProperty("url"));

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

 

4<url-pattern>路径

 

        要么以“*”开关,要么为“/”开头,当通常情况看下,我们都会以"/"开头。

  • 17
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值