在Servlet中获取来源URL,即发送当前请求的URL。比如从a.jsp进入Servlet,在Servlet中可以获得a.jsp的路径。以下是代码:
Java代码
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>请求来源</TITLE></HEAD>");
out.println(" <BODY>");
String fromURL = request.getHeader("Referer");
out.print("来源于:"+fromURL);
StringBuffer requestURL = request.getRequestURL();
String queryString = request.getQueryString();
if(queryString != null){
requestURL .append("?").append(queryString);
}
out.print("<br />请求:"+requestURL.toString());
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>请求来源</TITLE></HEAD>");
out.println(" <BODY>");
String fromURL = request.getHeader("Referer");
out.print("来源于:"+fromURL);
StringBuffer requestURL = request.getRequestURL();
String queryString = request.getQueryString();
if(queryString != null){
requestURL .append("?").append(queryString);
}
out.print("<br />请求:"+requestURL.toString());
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} 从代码中可以看出获取来源URL,是通过获取HTTP头的方式得到的,代码中String fromURL = request.getHeader("Referer"); 语句。而request.getRequestURL()是获取请求的URL,不包括参数。request.getQueryString()用于获取参数信息。
以下附录在编程常要获取的,包括获取一些常用的HTTP头:
Java代码
Java代码
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>请求来源</TITLE></HEAD>");
out.println(" <BODY>");
String fromURL = request.getHeader("Referer");
out.print("来源于:"+fromURL);
StringBuffer requestURL = request.getRequestURL();
String queryString = request.getQueryString();
if(queryString != null){
requestURL .append("?").append(queryString);
}
out.print("<br />请求:"+requestURL.toString());
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>请求来源</TITLE></HEAD>");
out.println(" <BODY>");
String fromURL = request.getHeader("Referer");
out.print("来源于:"+fromURL);
StringBuffer requestURL = request.getRequestURL();
String queryString = request.getQueryString();
if(queryString != null){
requestURL .append("?").append(queryString);
}
out.print("<br />请求:"+requestURL.toString());
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} 从代码中可以看出获取来源URL,是通过获取HTTP头的方式得到的,代码中String fromURL = request.getHeader("Referer"); 语句。而request.getRequestURL()是获取请求的URL,不包括参数。request.getQueryString()用于获取参数信息。
以下附录在编程常要获取的,包括获取一些常用的HTTP头:
Java代码
out.println("Protocol: " + request.getProtocol());
out.println("Scheme: " + request.getScheme());
out.println("Server Name: " + request.getServerName() );
out.println("Server Port: " + request.getServerPort());
out.println("Protocol: " + request.getProtocol());
out.println("Server Info: " + getServletConfig().getServletContext().getServerInfo());
out.println("Remote Addr: " + request.getRemoteAddr());
out.println("Remote Host: " + request.getRemoteHost());
out.println("Character Encoding: " + request.getCharacterEncoding());
out.println("Content Length: " + request.getContentLength());
out.println("Content Type: "+ request.getContentType());
out.println("Auth Type: " + request.getAuthType());
out.println("HTTP Method: " + request.getMethod());
out.println("Path Info: " + request.getPathInfo());
out.println("Path Trans: " + request.getPathTranslated());
out.println("Query String: " + request.getQueryString());
out.println("Remote User: " + request.getRemoteUser());
out.println("Session Id: " + request.getRequestedSessionId());
out.println("Request URI: " + request.getRequestURI());
out.println("Servlet Path: " + request.getServletPath());
out.println("Accept: " + request.getHeader("Accept"));
out.println("Host: " + request.getHeader("Host"));
out.println("Referer : " + request.getHeader("Referer"));
out.println("Accept-Language : " + request.getHeader("Accept-Language"));
out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding"));
out.println("User-Agent : " + request.getHeader("User-Agent"));
out.println("Connection : " + request.getHeader("Connection"));
out.println("Cookie : " + request.getHeader("Cookie"));
out.println("Created : " + session.getCreationTime());
out.println("LastAccessed : " + session.getLastAccessedTime());