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 ...
随机推荐
- 性能测试工具LoadRunner09-LR之Virtual User Generator 日志
在录制和回放的时候,VU会分别把发生的事件记录成日志文件,这些日志有利于我们跟踪VU和服务器的交互过程 回放日志(Replay Log) 脚本回放运行时的输出都记在这个Log里 “输出”窗口的“回放日 ...
- [转]jquery插件创建 jquery.fn.extend与jquery.extend
本文转自:http://www.cnblogs.com/wyjgreat/archive/2011/07/19/2110754.html jQuery为开发插件提拱了两个方法,分别是: JavaScr ...
- If you are tired...
如果你累了 1. 深呼吸 放松身体,深呼吸五分钟. 2. 听音乐 静静地听几首歌放松一下就好了,比如王豪学长推荐的追梦赤子心,骄傲的少年. 3. 冥想 放松身体,处于冥想状态. 4. 干洗脸.鸣天鼓. ...
- jquery中load()加载页面,刷新之后,加载的页面不显示的解决办法
<script language="javascript" type="text/javascript"> $(function(){ $(&quo ...
- 【转】浅谈https\ssl\数字证书
转载请注明出处:http://www.cnblogs.com/P_Chou/archive/2010/12/27/https-ssl-certification.html 全球可信的SSL数字证书申请 ...
- 修改默认 JVM 参数 securerandom.source
最近在云服务器上部署tomcat的时候,执行 startup.sh老是卡住了. 开始以为是日志配的不对,因为在对象中引入的是slf4,不是apache的log4j,后面把所有引入的地方都修改为Apac ...
- pymongo模块
import pymongo # 创建与MongoDB服务器的连接 mongoclient = pymongo.MongoClient(host='127.0.0.1', port=27017) # ...
- Git如何解决冲突
解决冲突 现在我把gitTest中的东西全删了包括那个.git文件. 初始化仓库git init,新建一个a.txt,在里边写个master,执行git add a.txt,然后执行git commi ...
- ios上【点击select元素,输入框自动获得焦点的问题】
今天遇到了一个很奇怪的问题:在ios手机上,如果页面出现滚动条,点击select元素的时候,偶尔会出现 “ 页面上的某一个输入框自动获得焦点 “ 的问题. 这个问题困扰我好久,一直找不到答案,今天终于 ...
- foxmail6.5 不能收取电子邮件,反复提示输入密码?
使用foxmail时候报错:-err system resource error,system close connect,code=<1014>,id=<1>重新输入密码吗? ...