温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

javaweb学习中的路径问题

发布时间:2020-08-10 03:38:31 来源:网络 阅读:325 作者:Adam的blog 栏目:开发技术

1. 项目结构

javaweb学习中的路径问题

2. 客户端路径

1. 超链接

<%@ 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/testPath/jsp/b.jsp">这是绝对地址超链接</a>
    <br />

    <!-- 2.以"/"开头的相对地址    -->
    <!-- /代表了整个web项目,即:http://localhost:8080/ -->
    <a href="/testPath/jsp/b.jsp">这是以"/"开头的相对地址超链接</a>
    <br />

    <!-- 3.不以"/"开头的相对地址   -->
    <!--   
                不以/开头,则相对于当前资源的路径  
                当前资源的路径为:http://localhost:8080/testPath/jsp/  
                而b.jsp也在此路径下  
                所以直接书写b.jsp  
     -->
    <a href="b.jsp">这是不以"/"开头的相对地址超链接</a>
    <br />
</body>
</html>

2. 表单

<%@ 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>页面b</title>
</head>
<body>
    <!-- 所有的表单都是提交到b.jsp -->
    <!--   
            表单提交路径有三种书写方式   
                1,绝对地址    
                2,以"/"开头的相对地址  
                3,不以"/"开头的相对地址   
    -->
    <form action="http://localhost:8080/testPath/jsp/b.jsp" methoe="get">
        username:<input type="text" name="username" value=""> <input
            type="submit" value="提交---绝对地址    ">
    </form>
    <!--   
            以/开头的相对地址,此时的/代表整个web项目,即:http://localhost:8080/  
    -->
    <form action="/testPath/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/testPath/PathServlet" methoe="get">
        username:<input type="text" name="username" value=""> <input
            type="submit" value="表单提交到Servlet---绝对地址">
    </form>

    <!-- 2.以/开头的相对地址  -->
    <!-- 此时的/代表整个web项目,即:http://localhost:8080/  -->
    <form action="/testPath/PathServlet" methoe="get">
        username:<input type="text" name="username" value=""> <input
            type="submit" value="表单提交到Servlet---以/开头的相对地址">
    </form>
    <!-- 3.不以/开头的相对地址    -->
    <!--   
                不以/开头的相对路径是相对于当前资源的路径  
                此时form.jsp的地址为:http://localhost:8080/testPath/jsp/form.jsp  
                所以当前资源路径为:http://localhost:8080/testPath/jsp  
                而要提交的Servlet的路径为Http://localhost:8080/testPath/PathServlet  
                所以路径为当前路径的上一级路径下  
                即路径为:../PathServlet  
                注:.代表当前路径   ..代表当前路径的上一级路径  
        -->
    <form action="../PathServlet" methoe="get">
        username:<input type="text" name="username" value=""> <input
            type="submit" value="表单提交到Servlet---不以/开头的相对地址">
    </form>
</body>
</html>

3.重定向

package cn.test.path;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Guozhen_Zhao
 * 创建时间:2018年3月17日  上午10:12:21
 * 备注:重定向有三种路径书写方式 : 
 *      1. 绝对路径 
 *      2. 以"/"开头的相对路径 
 *      3. 不以"/"开头的相对路径 
 */
@WebServlet("/RedirectServlet")
public class RedirectServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.sendRedirect("http://localhost:8080/testPath/jsp/b.jsp");
        /* 
         * 2.以"/"开头的相对路径 
         *  此时,/代表整个web工程的路径,即http://localhost:8080/ 
         */
        //        response.sendRedirect("/testPath/jsp/b.jsp"); 

        /* 
         * 3.不以"/"开头的相对路径 
         *      此时是相对于当前资源的相对路径 
         *      当前资源路径为:http://localhost:8080/testPath/RedirectServlet 
         *      即表示:RedirectServlet在路径http://localhost:8080/testPath之下 
         *      而b.jsp在http://localhost:8080/testPath/jsp/b.jsp 
         *      所以最终地址写为:jsp/b.jsp 
         */
        //        response.sendRedirect("jsp/b.jsp"); 
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

服务器端路径

请求转发

package cn.test.path;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Guozhen_Zhao
 * 创建时间:2018年3月17日  上午10:09:59
 * 备注: 服务器端的路径不能是绝对路径,只能是相对路径,也分为以/开头和不以/开头两种 
 *      1.以"/"开头的相对路径 
 *      2.不以"/"开头的相对路径
 */
@WebServlet("/DispatcherServlet")
public class DispatcherServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected 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);  
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI