知行合一

恋結びの夏 私は、彼女

   :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在Servlet中获取来源URL,即发送当前请求的URL。比如从a.jsp进入Servlet,在Servlet中可以获得a.jsp的路径。以下是代码:

 1     public void doGet(HttpServletRequest request, HttpServletResponse response)  
 2             throws ServletException, IOException {  
 3         request.setCharacterEncoding("utf-8");  
 4         response.setContentType("text/html;charset=utf-8");  
 5         PrintWriter out = response.getWriter();  
 6         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");  
 7         out.println("<HTML>");  
 8         out.println("  <HEAD><TITLE>请求来源</TITLE></HEAD>");  
 9         out.println("  <BODY>");  
10         String fromURL = request.getHeader("Referer");        
11         out.print("来源于:"+fromURL);  
12         StringBuffer requestURL = request.getRequestURL();  
13         String queryString = request.getQueryString();  
14         if(queryString != null){  
15             requestURL .append("?").append(queryString);  
16         }  
17         out.print("<br />请求:"+requestURL.toString());  
18         out.println("  </BODY>");  
19         out.println("</HTML>");  
20         out.flush();  
21         out.close();  
22     }  

 

 从代码中可以看出获取来源URL,是通过获取HTTP头的方式得到的,代码中String fromURL = request.getHeader("Referer");  语句。而request.getRequestURL()是获取请求的URL,不包括参数。request.getQueryString()用于获取参 数信息。

 以下附录在编程常要获取的,包括获取一些常用的HTTP头:

 1     out.println("Protocol: " + request.getProtocol());   
 2     out.println("Scheme: " + request.getScheme());   
 3     out.println("Server Name: " + request.getServerName() );   
 4     out.println("Server Port: " + request.getServerPort());   
 5     out.println("Protocol: " + request.getProtocol());   
 6     out.println("Server Info: " + getServletConfig().getServletContext().getServerInfo());   
 7     out.println("Remote Addr: " + request.getRemoteAddr());   
 8     out.println("Remote Host: " + request.getRemoteHost());   
 9     out.println("Character Encoding: " + request.getCharacterEncoding());   
10     out.println("Content Length: " + request.getContentLength());   
11     out.println("Content Type: "+ request.getContentType());   
12     out.println("Auth Type: " + request.getAuthType());   
13     out.println("HTTP Method: " + request.getMethod());   
14     out.println("Path Info: " + request.getPathInfo());   
15     out.println("Path Trans: " + request.getPathTranslated());   
16     out.println("Query String: " + request.getQueryString());   
17     out.println("Remote User: " + request.getRemoteUser());   
18     out.println("Session Id: " + request.getRequestedSessionId());   
19     out.println("Request URI: " + request.getRequestURI());   
20     out.println("Servlet Path: " + request.getServletPath());   
21     out.println("Accept: " + request.getHeader("Accept"));   
22     out.println("Host: " + request.getHeader("Host"));   
23     out.println("Referer : " + request.getHeader("Referer"));   
24     out.println("Accept-Language : " + request.getHeader("Accept-Language"));   
25     out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding"));   
26     out.println("User-Agent : " + request.getHeader("User-Agent"));   
27     out.println("Connection : " + request.getHeader("Connection"));   
28     out.println("Cookie : " + request.getHeader("Cookie"));   
29     out.println("Created : " + session.getCreationTime());   
30     out.println("LastAccessed : " + session.getLastAccessedTime());  
posted on 2012-04-29 01:55  PeteYan  阅读(9678)  评论(1编辑  收藏  举报