标签:nbsp utf-8 meta doctype doc bubuko alt 实例 static
来看具体实例:
在HTML页面写两个a链接请求,这个就是get请求了,然后建立与请求对应的servlet,页面代码如下所示:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>请求转发实例</title> 6 </head> 7 <body> 8 <h2 style="text-align: center;">Forward测试页面</h2> 9 <a href="fowardServlet">foward请求</a> 10 <br/></br/> 11 <a href="sendServlet">send重定向</a> 12 </body> 13 </html>
1 package com.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.RequestDispatcher; 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class ForwardServlet extends HttpServlet { 12 private static final long serialVersionUID = 1L; 13 14 protected void doGet(HttpServletRequest request, HttpServletResponse response) 15 throws ServletException, IOException { 16 request.setAttribute("forward_name", "abcde"); 17 System.out.println("请求转发获取的属性:" + request.getAttribute("forward_name")); 18 System.out.println("ForwardServlet 的 doGet()方法"); 19 // 转发请求 20 String path = "testServlet"; 21 RequestDispatcher requestDispatcher = request.getRequestDispatcher("/" + path); 22 requestDispatcher.forward(request, response); 23 } 24 25 }
1 package com.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 @WebServlet(name = "sendServlet", urlPatterns = "/sendServlet") 12 public class SendServlet extends HttpServlet { 13 private static final long serialVersionUID = 1L; 14 15 protected void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 request.setAttribute("send_name", "abcde"); 18 System.out.println("重定向获取的属性:" + request.getAttribute("send_name")); 19 System.out.println("SendServlet 的doGet 方法"); 20 String path = "testServlet"; 21 response.sendRedirect(path); 22 23 } 24 25 }
1 package com.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 @WebServlet("/testServlet") 12 public class TestServlet extends HttpServlet { 13 private static final long serialVersionUID = 1L; 14 15 protected void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 // TODO Auto-generated method stub 18 System.out.println("TestServlet 的 doGet 方法"); 19 20 System.out.println("测试请求转发获取的属性:" + request.getAttribute("forward_name")); 21 22 System.out.println("测试重定向获取的属性:" + request.getAttribute("send_name")); 23 } 24 25 }
(1) 请求转发forward 只有一次请求,而重定向是两次请求:
(2)请求转发:在最终的 servlet (TestServlet) 中,request和中转的那个servlet(ForwardServlet)中的request对象是同一个对象
重定向:在最终的 servlet (TestServlet) 中,request和中转的那个servlet(SendServlet)中的request对象不是同一个对象
(3)请求转发:地址栏中的url是初次发起请求的地址
重定向:地址栏中的url是最终响应的的那个地址
(3)请求转发:仅限于当前web应用内,"/"代表当前web应用的根目录
请求重定向:可以定向到任何资源,"/"代表当前web站点的根目录
标签:nbsp utf-8 meta doctype doc bubuko alt 实例 static
原文地址:https://www.cnblogs.com/yaoruozi/p/8878581.html