链接过来的时候,referer的值是链接过来的页面的URL,正常。我刷新的时候referer的值没有变,但当我在地址栏中打回车(URL没有改变,只是打回车),referer值却变成NULL了。
1):链接过来和在地址栏中直接输入URL,有没有什么区别?如果有,有什么区别?
2):刷新的时候URL为什么不变,为什么还是前一个URL地址,为什么不变成当前的URL?
3):直接输入为什么得不到 <%=request.getHeader("REFERER")的值?
如果是直接在浏览器里输入有referer的页面,返回是null,也就是说referer只有从别的页面点击连接来到这页的才会有内容。为NULL就是手工,非null就是连接过来的。刷新的时候,会检查服务端是否会有更新,没有的话,则使用本机的缓存,也就是说,你刷新时得到的响应依然是
<%@ page import="java.util.*" %>
<html>
<head>
<title>Echo</title>
<style>
<jsp教程:include page="style.css教程" flush="true"/>
</style>
</head>
<body>
<h1>HTTP Request Headers Received</h1>
<table border="1" cellpadding="4" cellspacing="0">
<%
Enumeration eNames = request.getHeaderNames();
while (eNames.hasMoreElements()) {
String name = (String) eNames.nextElement();
String value = normalize(request.getHeader(name));
%>
<tr><td><%= name %></td><td><%= value %></td></tr>
<%
}
%>
</table>
</body>
</html>
<%!
private String normalize(String value)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
sb.append(c);
if (c == ';')
sb.append("<br>");
}
return sb.toString();
}
%>综合实例
index.jsp
<HTML>
<HEAD>
<TITLE>Getting Header Data</TITLE>
</HEAD>
<BODY>
<H1>Getting Header Data</H1>
<FORM ACTION="basic.jsp" METHOD="POST">
<INPUT TYPE="submit" VALUE="Submit">
</FORM>
</BODY>
</HTML>
basic.jsp
<HTML>
<HEAD>
<TITLE>Reading Header Information</TITLE>
</HEAD><BODY>
<H1>Reading Header Information</H1>
Here are the request headers and their data:
<BR>
<% java.util.Enumeration names = request.getHeaderNames();
while(names.hasMoreElements()){
String name = (String) names.nextElement();
out.println(name + ":<BR>" + request.getHeader(name) + "<BR><BR>");
}
%>
</BODY>
</HTML>
通过方法可以知道访问者的request.getHeader("Referer");其他的方法下列
System.out.println("Protocol: " + request.getProtocol());
System.out.println("Scheme: " + request.getScheme());
System.out.println("Server Name: " + request.getServerName() );
System.out.println("Server Port: " + request.getServerPort());
System.out.println("Protocol: " + request.getProtocol());
System.out.println("Server Info: " + getServletConfig().getServletContext().getServerInfo());
System.out.println("Remote Addr: " + request.getRemoteAddr());
System.out.println("Remote Host: " + request.getRemoteHost());
System.out.println("Character Encoding: " + request.getCharacterEncoding());
System.out.println("Content Length: " + request.getContentLength());
System.out.println("Content Type: "+ request.getContentType());
System.out.println("Auth Type: " + request.getAuthType());
System.out.println("HTTP Method: " + request.getMethod());
System.out.println("Path Info: " + request.getPathInfo());
System.out.println("Path Trans: " + request.getPathTranslated());
System.out.println("Query String: " + request.getQueryString());
System.out.println("Remote User: " + request.getRemoteUser());
System.out.println("Session Id: " + request.getRequestedSessionId());
System.out.println("Request URI: " + request.getRequestURI());
System.out.println("Servlet Path: " + request.getServletPath());
System.out.println("Accept: " + request.getHeader("Accept"));
System.out.println("Host: " + request.getHeader("Host"));
System.out.println("Referer : " + request.getHeader("Referer"));
System.out.println("Accept-Language : " + request.getHeader("Accept-Language"));
System.out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding"));
System.out.println("User-Agent : " + request.getHeader("User-Agent"));
System.out.println("Connection : " + request.getHeader("Connection"));
System.out.println("Cookie : " + request.getHeader("Cookie"));
System.out.println("Created : " + session.getCreationTime());
System.out.println("LastAccessed : " + session.getLastAccessedTime());