request对象和response对象(8)
来源:未知 责任编辑:责任编辑 发表时间:2014-05-10 12:18 点击:次
p>/Request/servlet/RequestDemo1 ---- 都是URI 不是URL
p>
p>2)获得当前访问资源路径 :
p>
p>request.getRequestURI().substring(request.getContextPath().length());
p>
p>2.获取请求头信息
p>
p>getHeader 获得头信息的值,转换一个字符串
p>
p>getHeaders 获得头信息值 ,获得Enumeration
p>
p>getHeaderNames 获得所有头信息名称 返回 Enumeration
p>
p>eg:
p>
p>复制代码
p>package cn.lsl.request;
p>
p>import java.io.IOException;
p>import java.util.Enumeration;
p>import javax.servlet.ServletException;
p>import javax.servlet.http.HttpServlet;
p>import javax.servlet.http.HttpServletRequest;
p>import javax.servlet.http.HttpServletResponse;
p>
p>public class RequestDemo2 extends HttpServlet {
p>
p> public void doGet(HttpServletRequest request, HttpServletResponse response)
p> throws ServletException, IOException {
p> //获取指定头信息字段
p> String value = request.getHeader("Accept-Encoding");
p> System.out.println(value);
p> System.out.println("---------------");
p> //获得所有头信息内容
p> Enumeration<String> enumeration = request.getHeaderNames();
p> while(enumeration.hasMoreElements()){
p> String name = enumeration.nextElement();
p> System.out.println(name+":"+request.getHeader(name));
p> }
p> }
p>
p> public void doPost(HttpServletRequest request, HttpServletResponse response)
p> throws ServletException, IOException {
p> doGet(request,response);
p> }
p>
p>}
p>复制代码
p>3.防盗链程序
p>
p>referer.html
p>
p>复制代码
p><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
p><html>
p> <head>
p> <title>referer.html</title>
p> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
p> <meta http-equiv="description" content="this is my page">
p> <meta http-equiv="content-type" content="text/html; charset=UTF-8">
p> </head>
p>
p> <body>
p> <h1>通过链接访问RequestDemo3 不是盗链</h1>
p> <a href="/Request/servlet/RequestDemo3">link</a>
p> </body>
p></html>
p>复制代码
p>复制代码
p>package cn.lsl.request;
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>