页面的跳转:转发
疑问
能否在OneServlet中保存值到请求域中,在另一个TwoServlet中打印出来?
如果是不同的请求,不能取出来。如果是同一次请求是可以取出来的。
转发与重定向的作用
在Servlet中实现页面的跳转有两种方式:转发和重定向
什么是转发
概念
由服务器端进行的页面跳转
原理图
转发的方法
案例
需求
实现从OneServlet中转发到TwoServlet
步骤
效果
代码
OneServlet
package com.itheima.servlet;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;/** 与请求域相关的方法 */@WebServlet(name = "Demo6OneServlet", urlPatterns = "/one")public class Demo6OneServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();out.print("这是One");//向请求域中添加键和值request.setAttribute("name","Jack");request.setAttribute("age", 20); //整数类型String name = (String) request.getAttribute("name");int age = (int) request.getAttribute("age");//从请求域中取出值out.print("名字:" + name + "");out.print("年龄:" + age );out.print("");//删除其中一个request.removeAttribute("age");//从请求域中取出值,直接输出Object对象out.print("名字:" + request.getAttribute("name") + "");out.print("年龄:" + request.getAttribute("age") );System.out.println("这是one");//开始转发,参数是:要跳转到的地址request.getRequestDispatcher("/two").forward(request, response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response);}}
TwoServlet
package com.itheima.servlet;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;//从请求域中取出值@WebServlet(name = "Demo7TwoServlet", urlPatterns = "/two")public class Demo7TwoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();out.print("这是Two,从请求域中取出one中的值:" + request.getAttribute("name"));}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response);}}
转发的特点
转发使用哪个方法?
request.getRequestDispatcher("/地址").forward(request, response);
页面的跳转:重定向
什么是重定向
概念
由浏览器端进行的页面跳转
原理图
重定向方法
重定向案例
需求
从OneServlet重定向到TwoServlet
步骤
效果
代码
OneServlet
package com.itheima.servlet;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;/** 与请求域相关的方法 */@WebServlet(name = "Demo6OneServlet", urlPatterns = "/one")public class Demo6OneServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();out.print("这是One");//向请求域中添加键和值request.setAttribute("name","Jack");request.setAttribute("age", 20); //整数类型String name = (String) request.getAttribute("name");int age = (int) request.getAttribute("age");//从请求域中取出值out.print("名字:" + name + "");out.print("年龄:" + age );out.print("");//删除其中一个request.removeAttribute("age");//从请求域中取出值,直接输出Object对象out.print("名字:" + request.getAttribute("name") + "");out.print("年龄:" + request.getAttribute("age") );System.out.println("这是one");//开始转发,参数是:要跳转到的地址//request.getRequestDispatcher("/two").forward(request, response);//使用重定向response.sendRedirect("two");}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response);}}
TwoServlet
package com.itheima.servlet;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;//从请求域中取出值@WebServlet(name = "Demo7TwoServlet", urlPatterns = "/two")public class Demo7TwoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();out.print("这是Two,从请求域中取出one中的值:" + request.getAttribute("name"));}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response);}}
重定向的特点
疑问
问:什么时候使用转发,什么时候使用重定向?
如果要保留请求域中的数据,使用转发,否则使用重定向。
以后访问数据库,增删改使用重定向,查询使用转发。
问:转发或重定向后续的代码是否还会运行?
无论转发或重定向后续的代码都会执行