連結過來的時候,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());