servlet 请求派发与重定向的区别理解

请求派发与重定向相同点:

两者均能指向内部地址,在网页设计中经常遇到这种场景,如在登录过程中判断用户是否登陆过,如果没登录过,则可以指向登录界面

请求派发与重定向不同点:

1.请求指派只能指派到内部地址,而重定向既可以指派的内部地址又可以指派到外部地址。

2.请求指派指派过程中url不变,用户无法感知,重定向url会改变

请求派发与重定向实现语法:

1.请求派发

request.getRequestDispatcher(url).forward(request, response);
其中url即可以是绝对路径又可以是相对路径

2.重定向

response.sendRedirect(url);
之中url如果指向内部地址则只能是相对路径


理论再多不如代码理解:

我将实现从page1.html,点击按钮submit,请求派发或重定向到page2.html的案例实现,如下图


先看下html的实现

page1代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    body{
      background-color: lightgreen;
    }
    h1{
      color: white;
    }
  </style>
</head>
<body>
  <h1>this is page one</h1>
  <form action="../first2.do" method="get">
    <input type="submit" value="submit">
  </form>
</body>
</html>

page2代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    body{
      background-color: purple;
    }
    h1{
      color: white;
    }
  </style>
</head>
<body>
  <h1>this is page two</h1>
</body>
</html>

FirstServlet代码(是已请求派送的方式实现):

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/first.do")
public class FirstServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//请求指派 HttpServletRequest getRequestDispatcher(url)方法
		request.getRequestDispatcher("/html/page2.html").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

FirstServlet2代码(是重定向方式实现,这里要修改page1.html表单的action属性action="../first2.do"):

package com.sxt.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/first2.do")
public class FirstServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//重定向 HttpServletResponse sendRedirect(url)方法
		//response.sendRedirect("http://www.baidu.com");//指向外部地址
		response.sendRedirect("html/page2.html"); //指向内部地址,只能用相对路径 
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

个人总结,有不对的地方请指出,多谢指教!

结语:No metter hao far you may fly , don't forgrt where you come from.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章