web项目路径问题
路径
相对路径
URL中第一个字符不为“/”
request.getRequestDispatcher("b");
相对于该代码所在的位置,指的是java文件的位置。
绝对路径
URL中第一个字符不为""
request.getRequestDispatcher("/b")
前台
/代表的是服务器的根目录,注意:是服务器的根目录
使用例子:
html页面,jsp页面,重定向
"ip:port/" http://localhost:8080/ webapps
后台
/代表的是项目的根目录 注意:是项目的根目录
使用例子:
request.getRequestDIspatcher("b")
web.xml
<url-pattern>/a</url-pattern>
http://localhost:8080/helloWorld/a
webapps/helloWorld ---hhtp://ip:port/helloWorld
Servlet
其路径由web.xml中的<url-pattern>/basic</url-pattern>
http://localhost:8888/jd1508/basic
路径并不是说他的类所在的目录
静态资源
其路径由其所在的项目的实际路径来决定,前提是没有放在WEB-INF下
webapps/jd1508/html/a.html
http://localhost:8888/jd1508/html/a.html
服务器根目录 webapps -- http://ip:port/
项目的根目录 webapps/jd1508 -- http://ip:port/jd1508
TOAHtmlServlet
http://localhost:8888/jd1508/toAHtml
--->
{ 重定向,day7/a.html 浏览器地址发生变化}
http://localhost:8888/jd1508/day7/a.html
a.html
---> TAServlet
http://localhost:8888/jd1508/ta
TOAHtmlServlet
http://localhost:8888/jd1508/toAHtml
--->
{ 内部跳转,/day7/a.html 浏览器地址不发生变化}
http://localhost:8888/jd1508/toAHtml
a.html
<a href="../a.html"></a>
---> TAServlet
http://localhost:8888/jd1508/ta
一个路径问题的小例子:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>a.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <base target="http://127.0.0.1:8080/ServletDemo02"> </head> <body> <!-- 需要访问到aServlet,怎么办呢?,第一种路径出现问题,表示 http://127.0.0.1:8080/aServlet--> <a href="/aServlet">AServlet</a><br/> <!-- 这种方式可以,最好是在jsp中使用,因为有时候你无法获知你的项目名 --> <a href="/ServletDemo02/aServlet">AServlet</a><br/> <!-- 这种相对路径的方式,先找到项目的根目录然后找到servlet,貌似可以,不过可能出现路径问题,问题会出现在哪呢? 因为相对上一级目录中查找对应的servlet,本次的路径不是固定的,一般使用内部跳转的话,浏览器的地址栏不会发生 变化,就会导致上一级目录发生变化而产生路径问题 http://127.0.0.1:8080/ServletDemo02/toAHtml http://127.0.0.1:8080/aServlet --> <a href="../aServlet">AServlet</a><br/> <!-- 我们可以使用base属性达到想要的效果,就是在页面设置一个基本地址,然后使用相对路径,设置base的target属性即可 。不过有时候即使设置了base,如果逻辑太过混乱也会出现路径问题,这个时候你就需要多琢磨了 --> <a href="aServlet">AServlet</a><br/> </body> </html> package com.servlet; 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 ToAHtml extends HttpServlet { private static final long serialVersionUID = 1L; public ToAHtml() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/html/a.html").forward(request, response); //response.sendRedirect("/html/a.html"); } public void init() throws ServletException { } } package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; public AServlet() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out=response.getWriter(); out.write("AServlet"); out.close(); } public void init() throws ServletException { } }
相关文章
- 玩转 Spring Boot 应用篇(项目打包、发布)(二十)
- intellij idea 配置svn及提交项目「建议收藏」
- 清华大学《数智安全与标准化》大作业项目征集说明(2022年秋季)
- 购物车项目代码+页面+连接数据库
- web基础知识_Web开发基础
- asp.net员工管理系统VS开发sqlserver数据库web结构c#编程计算机网页项目
- asp.net web项目 绑定ip地址运行方式
- 如何运行SpringBoot项目
- Tornado框架的异步代码单元支持同步获取URL在项目里实战的心得和方法
- django 1.11 项目迁移到 2.2 注意事项
- 上搭建web服务实现Web服务——学习Linux基础技能(在linux基础)
- 器Linux下检查Web服务器状态的方法(linux查看web服务)
- Linux安装Web环境:一步一步指导(linux安装web环境)
- 每个开源项目都离不开的三要素
- 2013年最不可思议的10个硬件开源项目
- 给开源项目贡献代码时:先讨论,再编码
- 基于C语言技术的Web应用程序开发与MySQL集成(c web mysql)
- 管理c语言与MySQL联合实现项目管理(c mysql 项目)
- 不能不用Redis无法实现项目(不用redis能做项目吗)
- 的应用Web开发中Redis的助力极大提升项目效率(web项目中redis)
- web项目快速部署之Redis配置指南(web项目redis配置)
- Web端快速读取Redis数据的实现方案(web读取redis数据)
- 红色的记忆Redis记录Web日志(redis记录web日志)