Servlet转发

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("通过通配符*的方式,所有路径都可以访问到");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值