JavaServlet 路径书写总结
在写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>路径
要么以“*”开关,要么为“/”开头,当通常情况看下,我们都会以"/"开头。
Servlet的路径跳转
一、JSP跳转到Servlet
1、相对路径,如href="servlet/TestServlet"
如果写成"/servlet/TestServlet"会报错,因为第一个“/”表示的是【服务器根目录】
2、绝对路径,通过内置成员变量path实现,如href="<%=path%>/servlet/TestServlet"
path得到的是项目根目录,如【http://localhost:8080/ServletDemo】
二、Servlet跳转JSP
1、请求重定向:response.sendRedirect(request.getContextPath()+"/xxx.jsp");
request.getContextPath()获得项目根目录,或者通过"../xxx.jsp"取得上层路径得到
2、服务器内部转发:
request.getRequestDispatcher("../xxx.jsp").forward(req,resp);
request.getRequestDispatcher("/test.jsp").forward(request, response); //斜线表示项目的根目录
小结:Servlet都可以通过../xxx.jsp获取路径
三、web.xml的路径
web.xml的<url-pattern>必须以“/”开头,“/”表示项目的根目录
在JSP页面上使用相对路径和绝对路径调用servlet
web.xml的<url-pattern>必须以“/”开头,“/”表示项目的根目录
如果在Servlet中使用请求重定向方式跳转到其他jsp页面,则需要:
response.sendRedirect(request.getContextPath()+"/test.jsp");
服务器内部跳转路径:
request.getRequestDispatcher("/test.jsp").forward(request,response);//这里的斜线表示项目的根目录
或者request.getRequestDispatcher("../test.jsp").forward(request,response);//“..”表示回到上层目录也就是根目录;
PS:如果<url-pattern>带一层路径 如:<url-pattern>/servlet/loginServlet</url-pattern>,则内部转发的时会从WebRoot/servlet/路径下找jsp页面,如果要转发的页面不在WebRoot/servlet/路径下,则需要“..”回到上层或根目录再定位到jsp页面,如下:request.getRequestDispatcher("../test.jsp").forward(request,response);
如果要转发的页面在WebRoot/servlet/路径下,则如下:request.getRequestDispatcher("/test.jsp").forward(request,response);
JavaServlet 路径书写总结的更多相关文章
- Thinkphp3.2.3路径书写注意
尽量不要这样写: ./public/img/a.jpg 应该这样写:__PUBLIC__/img/a.jpg 不然会引起不兼容 如首页地址 http://192.168.1.100/rjshop/时
- Tymeleaf模板引擎背景图片路径书写方式
<body style="background: url(../static/assets/img/bg-so-white.png);" th:style="'ba ...
- Servlet--表单、超链接、转发、重定向4种情况的路径
Servlet中相对路径总结 假设web工程使用如下目录结构: 在介绍相对路径和绝对路径前需要先了解几个概念: 服务器的站点根目录:以tomcat服务器为例,tomcat服务器站点根目录就是apach ...
- 论Mac与windows的STS下的路径问题
mac下的 <!-- javaBean生成在哪里 --> <javaModelGenerator targetPackage="com.atcrowdfunding.bea ...
- sea.js详解
Seajs相关知识 seajs.Use 引入入口文件 第一个参数表示模块id 字符串表示一个模块id 数组,数组每个成员表示一个模块 第二个参数表示回调函数(可有可无的) 作用就是当模块加载完成执行回 ...
- React Native常用组件Image使用
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- 初学者-微信小程序 问题解决办法记录
1.tabBar不显示的问题 1),检查大小写 2),pagePath路径书写,和pages路径一样,不能多或者少一个"/"或者"?" 2.tabBar和nav ...
- express+gulp构建项目(三)gulp任务
这次来看一看gulp是怎么工作的. tasks/paths.js paths.js文件里存放的是gulp任务中需要导入的文件的路径和导出的路径. /** * gulp.src 地址 * gulp.de ...
- 第九十九天上课 PHP TP框架 数据库查询和增加
在Model文件夹下创建模型,文件命名规则 : 表名Model.class.php <?php namespace Home\Model; use Think\Model; class yong ...
随机推荐
- (转)sdd for aix 安装及基本命令
总结出自多个文件(自己做的项目和网上找的资料) 原文:http://blog.csdn.net/yujin2010good/article/details/11395701 一.sddpcm安装 要安 ...
- LeetCode 704.二分查找(C++)
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1: 输入: num ...
- java语言编程实现两个时间相差多少天、多少小时、多少分、多少秒
不多说,直接上干货! DateDistance.java package zhouls.bigdata.DataFeatureSelection.test; import java.text.Date ...
- 用一层for循环初始化三维数组
][][]; ; i < * * ; i++) { a[i / ][(i / ) % ][i % ] = i; printf(, (i / ) % , i % ); // printf(&quo ...
- linux_api之文件属性
本篇索引:1.引言2.文件类型3.获取文件属性的函数,stat.fstat.lstat4.超级用户(root用户)和普通用户5.进程与用户ID6.文件权限的检查7.新创建的的文件和目录的所有权8.ac ...
- Spring boot-(1) Spring Boot快速开始
本系列博客将学习并运用Spring Boot, 尽量会涉及到Spring Boot的方方面面,且会参考Spring Boot的官方文档,相关英文书籍,以及相关博客等知识,争取做到详尽易懂. 1. Sp ...
- Intellij IDEA +genymotion安装配置
Intellij IDEA是一款非常好用的java编辑器,比Eclipse的代码提示要友善的多,优缺点不多讲了. 下面讲如何安装配置. 1)如官网下载最新版本 http://www.jetbrains ...
- 深入理解java虚拟机阅读笔记(1)运行时数据区域
java虚拟机所管理的内存区域主要分为方法区.堆:虚拟机栈.本地方法栈.程序计数器,如图: 1.程序计数器是当前线程所执行的字节码行号指示器,用以记录当前指令执行的位置.程序计数器是线程私有的,每个线 ...
- Form 头行附件查询
查询Form的头行附件: SELECT st.short_text order_short_text, description order_attach_desc, pk1_value order_ ...
- Html编码(&#数字型)与解码小结 - 针对Puny Code(中文域名)的解码处理
学习并了解到Html编码的知识,源于工作中的产品需求.如果一个URL里面包含Puny Code(不仅仅指中文,还可能是韩文等Unicode里非英文的国家文字,本文以含中文的URL为例),而且这个URL ...