[[390002]]
创新互联建站是一家专业提供满城企业网站建设,专注与成都网站设计、做网站、H5页面制作、小程序制作等业务。10年已为满城众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。
本文转载自微信公众号「见贤思编程」,作者泰斗贤若如。转载本文请联系见贤思编程公众号。
转发与重定向简介
转发和重定向都是实现页面跳转
也就是说,当我们访问一个 Servlet 的时候 ,Servlet 帮我们跳转到另一个界面。
转发与重定向的区别
代码演示转发和重定向
- 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" %>
登录
账号: 密码:
转发和重定向如何带数据到某个页面
- 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 加法计算器
- 加数1:
- 加数2:
count.jsp
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title - 计算结果:<%=request.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);
- }
- }
分享名称:聊聊Java中的转发与重定向
网页地址:http://www.shufengxianlan.com/qtweb/news23/299073.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联