forward与include的区别: forward方法是把请求的内容转发到另外的一个servlet.而include是把另一个servlet处理过后的内容拿过来
//存入一个值
request.setAttribute("username", "redarmychen");
//转发 请求分配
PrintWriter out = response.getWriter();
out.println("hihi");
RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
rd.forward(request, response);
在index.jsp中<body></body>中输入
<h1>${username}</h1>
在地址栏里输入http://localhost:8080/090201/rd.do
运行之后,地址栏变成http://localhost:8080/090201/index.jsp
输出结果是: redarmychen
而把forward改成include
输出结果就会变成:
Hihi
redarmychen