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 ...
随机推荐
- Java 实践
/** *宠物就是一个标准,包含多类宠物 *定义宠物标准接口Pet *定义Cat和Dog两个Pet接口的子类 *使用链表结构动态存储宠物信息 *定义商店类(工厂类),负责宠物的上架(链表添加).下架( ...
- E. Three strings 广义后缀自动机
http://codeforces.com/problemset/problem/452/E 多个主串的模型. 建立一个广义后缀自动机,可以dp出每个状态的endpos集合大小.同时也维护一个R[]表 ...
- python-常用模块整理
学习背景 最近需要进行文件夹数据的批量读取,里面会用到python里面的os模块.但是对os模块又不是很熟悉,里面的函数有哪些函数?有什么用?怎么用?英语不好的每次看官方文档也比较费力,所以就想着看看 ...
- Java基础03-类型转换
1.自动转换 int->double char->String 例:double a=10; 2.强制类型转换 (类型名)表达式 注意:String不能强制转化为char public c ...
- [转]使用Node.js完成的第一个项目的实践总结
本文转自:http://blog.csdn.net/yanghua_kobe/article/details/17199417 https://github.com/yanghua/FixedAsse ...
- Murano Weekly Meeting 2015.10.06
Meeting time: 2015.October.6th 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summar ...
- DEDE日期调用小插件
在日期文本框里面,点击的时候,下面出来一个和万年历一样的日期选择表,在dede里面,有一个现成的js小插件,直接调用就OK了... <input type="text" on ...
- 阿里云主机windows系统Apache启用浏览器缓存的方法
一群友使用卡卡网的网站速度诊断工具诊断网站速度时,发现有几个需要优化的地方,其中较为重要的是“启用浏览器缓存”.诊断结果显示,网站尚未启用浏览器缓存. 图一:浏览器缓存未启用 群友找我帮忙设置一下,据 ...
- Devexpress Xtrareports 创建多栏报表
根据官方回答:多列或多行(取决于当前的多栏设置)呈现数据的报表 这种报表是有用的,例如,当每个明细区都只显示少量数据.并且需要在一列的右侧打印下一个明细区时,这样就能充分利用整个页面的宽度,此外,当创 ...
- [精校版]The Swift Programming Language--语言指南--字符串和字符 (转)
今天装了10.10.马上就可以实际编写swift了.还是很兴奋啊. 哈哈.字符串和字符是大家最容易打交道的.今天就转一下讲解swift中字符串和字符的文章.希望对大家有帮助. 原文地址:http:// ...