zoukankan      html  css  js  c++  java
  • 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

    一个路径问题的小例子:

    <!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 {
        }
    
    }
    View Code
  • 相关阅读:
    默认路由
    路由与交换,cisco路由器配置,动态路由协议—RIP
    路由与交换,cisco路由器配置,浮动静态路由
    路由与交换,cisco路由器配置,静态路由
    路由与交换,cisco路由器配置,基础知识点(二)
    路由与交换,cisco路由器配置,基础知识点(一)
    Linux服务器架设篇,Nginx服务器的架设
    BIOS和DOS中断大全
    一步一个脚印
    汇编缩写大全
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/5671840.html
Copyright © 2011-2022 走看看