网站开发过程中的URL写法

在开发网页和服务器时发现,在很多地方需要写超链接

那么可以将超链接的使用者分为服务器和浏览器,以区分不同的写法

地址可能使用的情况:

1.跳转

2.转发

3.服务器资源地址

4.浏览器超链接

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 ServletDemo1 extends HttpServlet {
    //各个不同的地址使用 在web中
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //1.如果是给服务器使用的 表示是当前web应用 如果是给浏览器使用 就是web网页
        //这个地方是给服务器使用 那么就是web应用
        request.getRequestDispatcher("/form1.html").forward(request, response);
        
        //2.这个地方执行的是转发操作 是提供给浏览器使用 那么这个地址是一个网页 就需要寻找web应用及其下面的网站
        response.sendRedirect("/day06/1.html");
        
        //3.获取这个资源的路径 地址是提供给服务器使用 调用内部的资源 那么/代表着是web应用
        this.getServletContext().getResource("/form1.html");
        
        //4.这个是提供给服务器使用 自然是需要资源 获取流
        this.getServletContext().getResourceAsStream("/dowload/test.jpg");
        
        /**5.这个路径时通过给浏览器使用 那么地址就应该是服务器地址完整路径
         *综合不同使用情况可以制定给浏览器使用的url 需要指出web用 给服务器使用的可以不必给出web应用路径
         *正斜杠是硬盘地址 反斜杠是web地址
         * <a href="/day06/form1.html">点点</a>
         * <form action="/day06/form1.html">
         */
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

}

不过需要记住的是,//一般用在硬盘读取地址

恐惧源于无知,代码改变世界
原文地址:https://www.cnblogs.com/ad-zhou/p/8714558.html