Servlet中获取来源URL

在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代码
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());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值