zoukankan      html  css  js  c++  java
  • java ee jsp servlet路径问题总结

    1. 根路径

         > 服务器根路径

              - url: http://localhost:8080/

              - /

         > 项目根路径

              - url: http://localhost:8080/14_path_war_exploded

              - /14_path_war_exploded

    2. 相对路径和绝对路径

         > 相对路径: 相对于当前位置进行定位的方式 (不以/开头,以.开头的路径)

                         通过相对路径不可以确定唯一资源

                           如:./index.html

                               规则:找到当前资源和目标资源之间的相对位置关系

                    ./:当前目录

                   ../:后退一级目录

                      /:根目录

         > 绝对路径: 与当前位置无关, 直接从服务器根路径进行定位的方式 (以/开头)

    通过绝对路径可以确定唯一资源

    如:http://localhost/day15/responseDemo2 /day15/responseDemo2

    规则:判断定义的路径是给谁用的?判断请求将来从哪儿发出

    给客户端浏览器使用:需要加虚拟目录(项目的访问路径)

    建议虚拟目录动态获取:request.getContextPath()

    <a> , <form> 重定向...

    resp.sendRedirect(req.getContextPath() + "/b/bb/bbb/bbb.html");

    给服务器使用:不需要加虚拟目录

    转发路径

    req.getRequestDispatcher("/b/bb/bbb/bbb.html").forward(req, resp);

    3. <base href="">标签, 用于在页面中指定当前位置的默认地址.

    <base> 标签为页面上的所有链接规定默认地址或默认目标。使用 <base> 标签可以改变这一点。浏览器随后将不再使用当前文档的 URL,而使用指定的基本 URL 来解析所有的相对 URL。

    <base> 标签必须位于 head 元素内部。

    绝对路径=base路径+引用路径(img_src,a_href等等内设置的路径)

    假设图像的绝对地址是:

    <img src="http://www.w3school.com.cn/i/pic.gif" />

    现在我们在页面中的 head 部分插入 <base> 标签,规定页面中所有链接的基准 url:

    <head>

    <base href="http://www.w3school.com.cn/i/" />

    </head>

    在上例中的页面上插入图像时,我们必须规定相对的地址,浏览器会寻找文件所使用的完整 URL:

    <img src="pic.gif" />

    4. 后台代码中的路径问题

        > web.xml中的路径, /表示项目根路径

        > 页面跳转时的路径问题

             - 请求转发, /代表是项目根路径

             - 响应重定向, /代表是服务器根路径

    路径相关代码

    前台的路径问题

    在前台代码中/代表服务器根路径 http://localhost:8080/

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Title</title>
        <!--base标签用于指定当前页面的相对位置, 可以简化相对路径的写法-->
        <base href="http://localhost:8080/04_path_war_exploded/" />
    </head>
    <body>
    <h1>AAAA</h1>
    <a href="">当前位置</a>  <!--http://localhost:8080/14_path_war_exploded/-->
    <a href="/">服务器根路径</a> <!--http://localhost:8080/-->
    <a href="/14_path_war_exploded">项目根路径</a><br>  <!--http://localhost:8080/14_path_war_exploded-->
    <!--相对路径和绝对路径-->
    <ul>
        <li><a href="b/bb/bbb/bbb.html">bbb.html - 相对路径</a></li> <!--http://localhost:8080/14_path_war_exploded/b/bb/bbb/bbb.html-->
        <li><a href="/14_path_war_exploded/b/bb/bbb/bbb.html">bbb.html - 绝对路径</a></li>
        <!--http://localhost:8080/14_path_war_exploded/b/bb/bbb/bbb.html-->
    </ul>
    </body>
    </html>

    后台配置文件及Servlet中的路径问题

    > 在web.xml中, /代表项目根路径http://localhost:8080/14_path_war_exploded

    > 在请求转发中, /代表项目根路径http://localhost:8080/14_path_war_exploded

    > 在响应重定向中, /代表服务器根路径http://localhost:8080/

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
            version="4.0">
        <servlet>
            <servlet-name>path</servlet-name>
            <servlet-class>com.bjsxt.servlet.PathServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>path</servlet-name>
            <!--
                /代表项目根路径
                虚拟目录
            -->
            <url-pattern>/c/cc/path</url-pattern>
        </servlet-mapping>
    </web-app>
    public class PathServlet extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("servlet执行了...");
            // 页面跳转, 跳转到bbb.html页面
            // 相对路径, 此处的当前位置是servlet的访问路径, 比较费劲, 建议使用绝对路径
            // req.getRequestDispatcher("../../b/bb/bbb/bbb.html").forward(req, resp);
            // 绝对路径, 还是以/开头, 此时, /代表项目根路径(http://localhost:8080/14_path_war_exploded)
            // req.getRequestDispatcher("/b/bb/bbb/bbb.html").forward(req, resp);
            
            // resp.sendRedirect("../../b/bb/bbb/bbb.html");
            // 响应重定向时, /代表服务器根路径(http://localhost:8080/)
            resp.sendRedirect(req.getContextPath() + "/b/bb/bbb/bbb.html");
        }
    }

    在登录问题中的路径跳转问题

    在以下跳转servlet问题中,有两种路径设置方法

    一是设定一个base标签为页面上的所有链接规定默认地址或默认目标。

    二是直接使用绝对路径

    <%@ page import="com.cw.pojo.User" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
        <%--<base href="http://localhost:8080/15_login_war_exploded/">--%>
    </head>
    <body>
        <h1>欢迎来到学生管理系统</h1>
        当前用户:
        <font color="red">
            <%=((User)request.getSession().getAttribute("User")).getRealname() %>
        </font>
        <%--<a href="logout" target="_top">安全退出</a>--%>
    <a href="/15_login_war_exploded/logout" target="_top">安全退出</a>
    </body>
    </html>
    <servlet>
            <servlet-name>logout</servlet-name>
            <servlet-class>com.cw.servlet.LogoutServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>logout</servlet-name>
            <url-pattern>/logout</url-pattern>
        </servlet-mapping>
  • 相关阅读:
    #include< >和#include“ ”的区别
    第2章 基础语法 -- 运算符
    hive on spark的安装及问题
    第2章 Python基础语法 -- 数据类型
    第2章 Python基础语法--Hello python
    IT资源专业搜索-www.easysoo.cn
    学习英语
    zookeeper 安装
    Hadoop2.6 Ha 安装
    Hadoop平台安装前准备
  • 原文地址:https://www.cnblogs.com/chenxiaoge/p/13335473.html
Copyright © 2011-2022 走看看