重定向

  HttpServletResponse对象的sendRedirect(java.lang.String location)方法称作重定向

  如果location地址前面加上“/”,则表示相对于Servlet容器的根来请求,比如http://localhost:8080;如果location地址前面没有加上“/”,则表示相对于当前请求的URI来寻找地址。

 

请求转发

  RequestDispatcher的:forward(ServletRequest request, ServletResponse response)方法叫做请求转发。

  

 

实验例子1:重定向和请求转发似乎都是造成页面跳转

  第一个页面first.jsp:

举例说明重定向和请求转发的区别

举例说明重定向和请求转发的区别
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'first.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <form action="Second">
    <input type="text" name="username">
    <input type="submit" value="submit">
    </form>
  </body>
</html>
举例说明重定向和请求转发的区别

相关文章:

  • 2021-05-02
猜你喜欢
  • 2021-11-22
  • 2021-07-07
  • 2022-01-02
相关资源
相似解决方案