加载web应用资源文件
一、资源跳转(跳转其他资源文件)
跳转到其他资源文件(跳转到html、跳转到servlet、跳转到jsp)
1.路径分类
服务器路径: /资源名 (目标资源给服务器使用)
浏览器路径: /web项目名/资源名 (目标资源给浏览器使用)
通用路径: ./资源名 (只要是加载web应用资源文件,都可用此路径)
网络资源路径: http://www.baidu.com (目标资源给浏览器使用)
2.路径的选择方式
思考:目标资源时候给谁使用的。
给服务器使用: / 表示web应用的目录下(WebRoot目录下)--服务器路径(/资源名)
给浏览器使用: / 表示webapps的目录下 -- 浏览器路径(/项目名/资源名)
服务器跳转可使用服务器路径、通用路径
浏览器跳转可使用浏览器路径、通用路径、网络路径
3.资源跳转方式
3.1 转发
转发:
方式一:
this.getServletContext().getRequestDispatcher("路径").forward(req, resp);
方式二:
req.getRequestDispatcher("路径").forward(req, resp);
路径选择:
转发是服务器跳转,使用服务器路径、通用路径
public class HelloServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//方式一服务器路径:
this.getServletContext().getRequestDispatcher("/target.html").forward(req, resp);
//方式二通用路径:
this.getServletContext().getRequestDispatcher("./target.html").forward(req, resp);
}
}
3.2 重定向
重定向
方式一:
resp.sendRedirect("路径");
方式二:
resp.setStatus(302);
resp.setHeader("location", "路径");
路径选择:
重定向是浏览器跳转,使用浏览器路径、通用路径、网络资源路径
public class HelloServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//方式一:浏览器路径
resp.sendRedirect("/FirstServlet/b.html");
//方式二:通用路径
resp.sendRedirect("./b.html");
}
}
3.3 表单提交
表单
<form action="路径" method="get" >
</form>
路径选择:
表单是浏览器跳转,使用浏览器路径、通用路径、网络资源路径
方式一:浏览器路径
<form action="/day11/target.html" method="get" >
</form>
方式二:通用路径
<form action="./target.html" method="get" >
</form>
3.4 a标签-html超链接
html的超链接属于浏览器跳转,使用浏览器路径、通用路径、网络资源路径
方式一:浏览器路径
<a href="/day11/taret.html">test</a>
方式二:通用路径
<a href="./taret.html">test</a>
sevlet写出的a标签,虽然在后台编码,但是最终由浏览器负责跳转。所以使用浏览器路径、
通用路径、网络资源路径
方式一:浏览器路径
public class HelloServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("<a href=\"/day11/target.html\">test</a>");
}
}
方式二:通用路径
public class HelloServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("<a href=\"./target.html\">test</a>");
}
}
二、客户端跳转和服务器跳转区别
客户端跳转:response的sendRedirect方法
a标签
form表达提交
location。Href=“”
服务器跳转:JSP的forward动作
Request。GetRequestDispatcher()。Forward()
1、客户端跳转
2、服务器跳转
3、区别
1、客户端跳转浏览器地址栏发送变化,服务器跳转不会发送变化
2、客户端跳转发送两次请求,服务器跳转一次请求
3、客户端跳转两次请求会浪费资源,但是减轻服务器压力
服务器跳转一次请求节省资源,但是加大服务器压力
三、post提交和get提交
1、post提交数据会通过网络其他方式提交,get通过地址栏传递数据。
2、post提交理论上没有长度上限,但实际上回收到网路、服务器、客户端等情况影响
get提交时通过地址栏,地址栏有长度上限值,所有提交数据一定有上限
3、post提交因为不通过地址栏,所有安全性更高。