package day10.think_about_path;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 关于在web中必须要解决的路径的问题:
* 我们都知道以/开头的是绝对路径;不以斜杠开头的是相对路径
* 相对路径的使用我们都相对熟悉,但是在web中的绝对路径应该
* 怎么使用呢?
*
* 有些时候我们的绝对路径的根目录是站点(例如:tomcat下的webapps文件夹;localhost:8080/ );
* 但是有的时候我们的绝对路径的根目录就是我们的项目(例如我们当前的项目名:localhost:8080/MyFirstServlet/ );
*
* 这主要取决于我们进行跳转时候的行为:
* 服务器行为: / 代表从当前项目的根目录开始;
* 浏览器行为: / 代表从当前站点的根目录开始;
*
*
*
* @author mzy
*
*/
public class PathDemo extends HttpServlet {
private static final long serialVersionUID = -5487353929038279489L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
/**
* 理解服务器行为和浏览器行为:
* 服务器行为是服务器代客户进行跳转
* 并且只是内容上进行拷贝显示,URL并不会改变
*
* 浏览器行为,是主观的用户进行操作
* 用户的操作,就不会局限在当前的项目下了,
* 而应该是一个大范围,在你的站点下面。
*/
// 目标地址是:/hello.html 直接在项目根目录下
/**
* 转发:转发是服务器的行为,所以/应该代表的是当前的项目路径。
*/
// request.getRequestDispatcher("/hello.html").forward(request, response);
/**
* 重定向:重定向是浏览器行为,所以/应该代表的是当前的站点,localhost:8080/
*/
// response.sendRedirect("/MyFirstServlet/hello.html");
/**
* html页面的超链接:hmtl中的超链接属于浏览器行为
*/
response.getWriter().write("<a href='/MyFirstServlet/hello.html'>link</a>");
/**
* html页面的表单的action:属于浏览器行为
*/
response.getWriter().write("<form action='/MyFirstServlet/hello.html'><input type='submit' /></form>");
// 总结:html中的都是浏览器主动进行发出的请求,所以都是浏览器行为
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
}
}