轉發和重定向詳解

轉發與重定向的區別

 

         前言:以前寫了幾篇JSP的博客,如今又回過頭來看Servlet,溫故而知新,再回顧回顧,總會有收穫的。之前學習Servlet感受內容不少,如今看的時候,其實也沒多少東西,只需知道Servlet的生命週期,Servlet的實現方式,ServletContext做用域,接收和響應,轉發和重定向,我以爲差很少夠用了。固然,要是細究的話也不止這些,我針對的是新手。html

 

轉發與重定向簡介

轉發和重定向都是實現頁面跳轉,也就是說,當咱們訪問一個Servlet 的時候,Servlet幫咱們跳轉到另外一個界面。java

 

轉發與重定向的區別

實現轉發調用的是HttpServletRequest對象中的方法瀏覽器

實現重定向調用的是HttpServletResponse對象中的方法服務器

 

轉發時瀏覽器中的url地址不會發生改變app

重定向時瀏覽器中的url地址會發生改變jsp

 

轉發時瀏覽器只請求一次服務器ide

重定向時瀏覽器請求兩次服務器post

 

轉發能使用request帶數據到跳轉的頁面學習

重定向能使用ServletContext帶數據到跳轉的頁面this

 

代碼演示轉發和重定向

 

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" %>

<html>

<head>

    <title>登陸</title>

</head>

<body>

<form action="/login">

    <table align="center">

        <tr>

            <td>帳號:</td>

            <td><input type="text" name="name"></td>

        </tr>

        <tr>

            <td>密碼:</td>

            <td><input type="text" name="password"></td>

        </tr>

        <tr>

            <td><input type="submit" value="登陸"></td>

            <td><input type="reset" value="重置"></td>

        </tr>

    </table>

</form>

</body>

</html>

  

 

轉發和重定向如何帶數據到某個頁面

 

 

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" %>

<html>

<head>

    <title>轉發和重定向傳代數據練習</title>

</head>

<body>

    <%

        //一、接收轉發傳代的數據

      String name =  (String) request.getAttribute("name");

      out.println("轉發傳代的數據:"+name);



    %>



</body>

</html>

  

 

 

 

 

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" %>

<html>

<head>

    <title>轉發和重定向傳代數據練習</title>

</head>

<body>

    <%

        //一、接收重定向傳代的數據

        String name1 = (String)application.getAttribute("name");

        out.println("重定向傳代的數據:"+name1);

    %>

</body>

</html>

  

 

練習

 

 

 

 

 

 

 

 

 

 

 

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

    <title>Title</title>

</head>

<body>

<form action="CountServlet" method="post">

    <h3>加法計算器</h3>

    加數1:<input type="number" name="one">

    加數2:<input type="number" name="two">

    <input type="submit" value="計算">

</form>

</body>

</html>

  

 

count.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>



<html>



<head>



    <title>Title</title>



</head>



<body>



計算結果:<%=request.getAttribute("count")%>



<!--計算結果:<%=application.getAttribute("count")%>-->



</body>



</html>

  

 

 

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);

}


}

  

結束語

Servlet分享到此結束,想了解更多的朋友能夠看以前總結過的博客《Servlet》,和《JSP》 ,更多精彩等你來學習。

 

*****************************************************************************************************

個人博客園地址:https://www.cnblogs.com/zyx110/

  本文已獨家受權給腳本之家(jb51net)公衆號獨家發佈

相關文章
相關標籤/搜索