request.getRequestDispatcher()的两个方法forward()/include()!!!

本文详细解释了Servlet中的请求转发和请求包含的概念及其区别。请求转发由下一个Servlet完成响应体,而请求包含则是两个Servlet共同完成响应体。此外还对比了请求转发与重定向的不同之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RequestDispatcher rd = request.getRequestDispatcher("/MyServlet");
请求转发: rd.forward( request , response );
请求包含: rd.include( request , response );

有时一个请求需要多个Servlet协作才能完成,所以需要在一个Servlet跳到另一个Servlet!
>一个请求跨多个Servlet,需要使用转发和包含。
>请求转发:由下一个Servlet完成响应体!当前Servlet可以设置响应头!(留头不留体)
>请求包含:由两个Servlet共同完成响应体!(留头又留体)
>无论是请求转发还是请求包含,都在一个请求范围内!使用同一个request和response!

request域
   void setAttribute(String name ,Object value)
   Object getAttribute(String name)
   >同一个请求范围内使用request.setAttribute()/request.getAttribute()来传值!前一个Servlet调用setAttribute()保存值,后一个Servlet调用getAttribute()获取值;

请求转发和重定向的区别:
>请求转发是一个请求一次响应,而重定向是两次请求两次响应。
>请求转发地址不变化,而重定向会显示后一个请求的地址
>请求转发只能转发到本项目其它Servlet,而重定向不只能重定向到本项目的其它Servlet,还能定向到其它项目
>请求转发是服务端行为,只需给出转发的Servlet路径,而重定向需要给出requestURI,既包含项目名!

 

来自网易博客:http://jamkong.blog.163.com/blog/static/2361940132014101841545703/

5 应用系统的设计与实现 5.1 开发环境简介 本应用系统采用Java语言进行开发,基于B/S架构,使用JDBC作为数据访问接口,集成开发环境为Eclipse。前端采用HTML、CSS和JavaScript技术进行页面设计,后端使用Servlet和JSP技术实现业务逻辑和页面交互。 5.1 系统总体功能模块 系统主要有图书类别管理、出版社管理、图书管理、仓库信息管理、进货管理、入库管理、销售管理、出库管理等几个功能模块。功能模块图如图5-1所示。 图5-1 系统功能模块图 5.2 用户登录模块的设计实现 用户登录模块用于验证用户的身份,只有合法的用户才能进入系统。系统界面截图如图5-2所示。 图5-2 用户登录界面 关键代码如下: java @WebServlet("/login") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); if ("admin".equals(username) && "123456".equals(password)) { HttpSession session = request.getSession(); session.setAttribute("user", username); response.sendRedirect("index.jsp"); } else { request.setAttribute("error", "用户名或密码错误"); request.getRequestDispatcher("login.jsp").forward(request, response); } } } 5.3 图书管理模块的设计与实现 图书管理模块用于对图书的基本信息进行管理,包括查询、录入、删除、修改等操作。系统界面截图如图5-3所示。 图5-3 图书管理界面 关键代码如下: java // BookServlet.java @WebServlet("/book") public class BookServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); if ("list".equals(action)) { List<Book> books = BookDAO.getAllBooks(); request.setAttribute("books", books); request.getRequestDispatcher("book_list.jsp").forward(request, response); } else if ("add".equals(action)) { request.getRequestDispatcher("book_add.jsp").forward(request, response); } else if ("save".equals(action)) { String bookName = request.getParameter("bookName"); String author = request.getParameter("author"); String isbn = request.getParameter("isbn"); double price = Double.parseDouble(request.getParameter("price")); int categoryId = Integer.parseInt(request.getParameter("categoryId")); int publisherId = Integer.parseInt(request.getParameter("publisherId")); Book book = new Book(); book.setBookName(bookName); book.setAuthor(author); book.setIsbn(isbn); book.setPrice(price); book.setCategoryId(categoryId); book.setPublisherId(publisherId); BookDAO.addBook(book); response.sendRedirect("book?action=list"); } } } 使用C语言和mysql
06-24
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <jsp:include page="check_logstate.jsp"></jsp:include> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!doctype html> <html lang="zh_CN"> <head> <base href="<%=basePath%>" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>欢迎使用后台管理系统</title> <link rel="stylesheet" href="asset/page/css/style.default.css" type="text/css" /> <link rel="stylesheet" href="asset/page/css/responsive-tables.css"> <script type="text/javascript" src="asset/page/js/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="asset/page/js/bootstrap.min.js"></script> </head> <body> <div class="mainwrapper"> <div class="maincontent"> <div class="maincontentinner"> <h4 class="widgettitle">学生请假列表</h4> <table id="dyntable" class="table table-bordered responsive"> <thead> <tr> <th style="text-align: center;"> </th> <th style="text-align: center;">学生用户</th> <th style="text-align: center;">宿舍</th> <th style="text-align: center;">开始时间</th> <th style="text-align: center;">结束时间</th> <th style="text-align: center;">原因</th> <th style="text-align: center;">状态</th> <th style="text-align: center;">操作</th> </tr> </thead> <c:forEach items="${outList}" var="out"> <tr style="text-align: center;"> <td style="text-align: center;"> </td> <td style="text-align: center;">${out.realname}</td> <td style="text-align: center;">${out.roomsname}</td> <td style="text-align: center;">${out.starttime}</td> <td style="text-align: center;">${out.endtime}</td> <td style="text-align: center;">${out.reason}</td> <td style="text-align: center;">${out.stats}</td> <td style="text-align: center;"> <c:if test="${out.stats eq '待审批' }"> <a href="out/stats.action?id=${out.outid}">处理</a>  </c:if><a href="out/deleteOut.action?id=${out.outid}" onclick="{if(confirm('确定要删除吗?')){return true;}return false;}">删除</a></td> </tr> </c:forEach> </tbody> </table> <div class="dataTables_info" id="dyntable_info" style="text-align: right;">${html }</div> </div> </div> </div> </body> </html> 学生用户不显示姓名
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值