1. 程式人生 > >request請求參數-防盜鏈

request請求參數-防盜鏈

request請求參數-防盜鏈

java源碼

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//防盜鏈[下載文件,必須從指定網站進入,否則不允許下載文件]

public class Rdome4 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //取得瀏覽器來自於何方
         String referer = request.getHeader("referer");

         //取得瀏覽器訪問的URL
        // String url = request.getRequestURL().toString();

        //判段 
        // if(referer != null &&  url.equals("http://localhost:8080/day04/index.html"))
         if(referer!=null && referer.equals("http://localhost:8080/day04/index.html"))
         {
             //轉發到download.html頁面
             this.getServletContext().getRequestDispatcher("/download.html").forward(request, response);
         }else
         {
            //轉發到ad.html頁面
             this.getServletContext().getRequestDispatcher("/ad.html").forward(request, response);
         }

    }

}

訪問頁面:

<!DOCTYPE html>
<html>
  <head>
    <title>index.html</title>
    <meta name="content-type" content="text/html; charset=UTF-8">   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  </head>

  <body>
    <a href="/day04/Rdome4">進入下載頁面</a>
  </body>
</html>

成功後跳轉下載頁面

<!DOCTYPE html>
<html>
  <head>
    <title>download.html</title>

    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">

    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

  <body>
    這是下載頁面<br>
  </body>
</html>

request請求參數-防盜鏈