您的位置:首页 > 产品设计 > UI/UE

深入感受JavaWeb开发内幕——使用Request对象设置防盗链

2013-10-22 17:45 423 查看
深入体验JavaWeb开发内幕——使用Request对象设置防盗链

http://www.verydemo.com/demo_c293_i5426.html

[b]使用Request对象设置页面的防盗链[/b]

      所谓的[b]防盗链就是当你以一个非正常渠道去访问某一个Web资源的时候,服务器会将你的请求忽略并且将你的当前请求变为按正常渠道访问时的请求并返回到相应的页面,用户只有通过该页面中的相关操作去访问想要请求的最终资源。[/b]

        例如,你有一个访问某资源的网址,但是你事先不知道这个网址是有[b]防盗链的,那么当你输入该网址时你可能会发现,并没有马上跳转到你想要的资源页面而是一些无关的信息页面,但是就是在这些信息页面中你发现有一个超链接或是其他操作可以跳转到你所访问的最终资源页面。[/b]

      这就是[b]防盗链技术了,好了来看一个具体应用:[/b]

  
Request.java

package net.csdn.request;import java.io.IOException;
import java.io.PrintWriter;import java.util.Enumeration
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Request extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {getDoorChain(request, response);}

private void getDoorChain(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String referer = request.getHeader("referer");
if(referer==null || !referer.endsWith("http://localhost:8080/Request/index.jsp")){
response.sendRedirect("http://localhost:8080/Request/index.jsp");
return;
}
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset =utf-8");
PrintWriter pw = response.getWriter();
pw.write("喜剧片《东成西就》");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}


index.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 'index.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>
这里是防盗链技术的应用检测! <br>
<a href ="/Request/Request" >喜剧片 </a>

</body>
</html>


效果如图:





例如我最终想要通过http://lcoalhost:8080/Request/Request这个网址获取到我想要的《东成西就》

的资源可是当我真正的输入这个网址时,却转到了:
http://localhost:8080/Request/index.jsp这个页面






只有当你点击“喜剧片”这个超链接时才会真正的得到你想要的资源页面即:

 





好了赶快自己动手试一试吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javaweb java 防盗链