本文转载自微信公众号「见贤思编程」,作者泰斗贤若如。转载本文请联系见贤思编程公众号。
转发与重定向简介
转发和重定向都是实现页面跳转
也就是说,当我们访问一个 Servlet 的时候 ,Servlet 帮我们跳转到另一个界面。
转发与重定向的区别
- 实现转发调用的是 HttpServletRequest 对象中的方法
- 实现重定向调用的是 HttpServletResponse 对象中的方法
- 转发时浏览器中的 url 地址不会发生改变
- 重定向时浏览器中的 url 地址会发生改变
- 转发时浏览器只请求一次服务器
- 重定向时浏览器请求两次服务器
- 转发能使用 request 带数据到跳转的页面
- 重定向能使用 ServletContext 带数据到跳转的页面
代码演示转发和重定向
- package 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;
-
-
- @WebServlet("/login")
- public class ServletDemo extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //获取表单提交过来的数据
- //getParameter()方法可以获取请求的参数信息
- String name = req.getParameter("name");
- String password = req.getParameter("password");
-
-
- //打印获取到的参数信息
- System.out.println("name:"+name);
- System.out.println("password:"+password);
-
-
- //如果name=admin,password=123,则跳转到succee.jsp,否则跳转到fail.jsp
- if("admin".equals(name)&&"123".equals(password)){
- //通过转发实现跳转
- req.getRequestDispatcher("/success.jsp").forward(req,resp);
- }else {
- //通过重定向实现跳转
- resp.sendRedirect("/fail.jsp");
- }
- }
-
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req, resp);
-
-
- }
- }
JSP代码
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
-
登录 -
-
- ="/login">
- <table align="center">
账号: "text" name="name"> 密码: "text" name="password"> "submit" value="登录"> "reset" value="重置"> - table>
-
-
-
转发和重定向如何带数据到某个页面
- package servlet;
- import javax.servlet.ServletContext;
-
- 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;
- @WebServlet("/login")
-
- public class ServletDemo extends HttpServlet {
-
- @Override
-
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //通过转发带数据
-
- req.setAttribute("name","张三");
-
- req.getRequestDispatcher("/send.jsp").forward(req,resp);
- }
- @Override
-
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-
- doGet(req, resp);
- }
-
- }
send.jsp
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
-
转发和重定向传代数据练习 -
-
- <%
- //1、接收转发传代的数据
- String name = (String) request.getAttribute("name");
- out.println("转发传代的数据:"+name);
-
-
- %>
-
-
-
-
- package servlet;
- import javax.servlet.ServletContext;
-
- 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;
- @WebServlet("/login")
-
- public class ServletDemo extends HttpServlet {
-
- @Override
-
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-
-
-
- //通过重定向带数据
-
- ServletContext servletContext = this.getServletContext();
-
- servletContext.setAttribute("name","王二麻子");
-
- resp.sendRedirect("/send2.jsp");
- }
- @Override
-
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-
- doGet(req, resp);
- }
-
- }
send2.jsp
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
-
转发和重定向传代数据练习 -
-
- <%
- //1、接收重定向传代的数据
- String name1 = (String)application.getAttribute("name");
- out.println("重定向传代的数据:"+name1);
- %>
-
-
练习
index.jsp
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
-
Title -
-
- ="CountServlet" method="post">
加法计算器
- 加数1:"number" name="one">
- 加数2:"number" name="two">
- "submit" value="计算">
-
-
-
count.jsp
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
-
Title -
-
- 计算结果:<%=request.getAttribute("count")%>
- --计算结果:<%=application.getAttribute("count")%>-->
-
-
Servlet
- package servlet;
- import javax.servlet.ServletContext;
- 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;
- @WebServlet("/CountServlet")
- public class CountServlet extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String one=request.getParameter("one");
- int o=Integer.parseInt(one);//强制转换,将String类型的数据转换成int类型
- String two=request.getParameter("two");
- int t=Integer.parseInt(two);//强制转换,将String类型的数据转换成int类型
- System.out.println(one+" "+two);
- int c=o+t;
- String co=String.valueOf(c);//将int类型的数据转换成String类型
- //转发,可以携带数据
- request.setAttribute("count",co);
- request.getRequestDispatcher("count.jsp").forward(request,response);
- //用于存放数据
- // ServletContext s=this.getServletContext();
- // s.setAttribute("count",co);
- //重定向只能依靠ServletContext获取数据
- // response.sendRedirect("count.jsp");
- System.out.println(co);
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doPost(request,response);
- }
- }