Servlet
转发【重点】
浏览器发送HTTP请求 => 服务器
服务器接收到请求之后内部资源处理跳转 => 服务器其他资源
服务器其他资源处理完毕之后,才通过原始的请求和响应返回数据 => 客户端特征:
- 转发是服务器行为
- 转发情况下浏览器有且只发送了一次请求
- 转发情况下浏览器的地址栏没有发生任何改变
- 转发情况下,浏览器的请求数据是被保存的,是通过 request 进行传递的
- 转发情况下有且只能转发 WEB Application 整个项目的内容资源
起始页
/**
* 测试转发起始页
*/
@WebServlet("/TestForwardServletFrom")
public class TestForwardServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
// 设置自定义请求参数,保存到 request 中
request.setAttribute("Forward", "转发成功");
// 获取请求调度并器将 request 和 response 转发给 TestForWardServlet2
request.getRequestDispatcher("TestForwardServletTo").forward(request, response);
}
}
目的页
/**
* 测试转发目的页
*/
@WebServlet("/TestForwardServletTo")
public class TestForwardServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
// 获取请求参数
Object context = request.getAttribute("Forward");
System.out.println(context);
response.setContentType("text/html; charset=utf-8");
response.getWriter().append("我是目的页,这是从起始页传来的数据:" + context);
}
}
URL 匹配原则
url-pattern匹配原则:
1、精确匹配
/具体名字
2、后缀名匹配
*.do *.action
*.xxx
主要是按照规定结尾的url-pattern都可以触发对应的Servlet程序
3、通配符匹配
/* 匹配所有请求,服务器端所有资源都可以匹配
精准匹配
案例代码
/**
* 精准查询
*/
@WebServlet("/PreciseServlet")
public class PreciseServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html; charset=utf-8");
response.getWriter().append("测试URL的精准查询:通过完整的协议+域名+端口+资源+参数");
}
}
后缀名匹配
案例代码
/**
* 后缀名匹配
*/
@WebServlet("*.do")
public class SuffixServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html; charset=utf-8");
response.getWriter().append("测试URL的后缀名匹配,通过指定的后缀名即可");
}
}
通配符匹配
/**
* 通配符匹配
*/
@WebServlet("/*")
public class WildcardServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
response.getWriter().append("通过通配符*的方式,所有路径都可以访问到");
}
}
版权属于:不冷
本文链接:https://www.buleng.xyz/archives/97/
转载时须注明出处及本声明