request.getContextPath():获得“/项目名”;例如最常见的:${pageContext.request.contextPath}/xxxServlet ;
已测实例:
<a href='${pageContext.request.contextPath}/ProductServlet?method=findByCid&cid=" + cat.cid + "'>" + cat.cname + "</a>
========ContextPath:/day51_store68Self
request.getRequestURL():获得完整请求路径名(注意不包括请求参数,如果带参数的话);
request.getRequestURI():只包括项目名及其后面的的 URL部分(也就是web资源路径);
已测实例:
========RequestURI:/day51_store68Self/IndexServlet
========RequestURL:http://localhost:8080/day51_store68Self/IndexServlet
request.getQueryString():问号后面内容。
已测实例:
<a href='${pageContext.request.contextPath}/ProductServlet?method=findByCid&cid=" + cat.cid + "'>" + cat.cname + "</a>
========QueryString:method=findByCid&cid=1
重复一遍:
request.getContextPath():获得“/项目名”;例如最常见的:${pageContext.request.contextPath}/xxxServlet ;
已测实例:
<a href='${pageContext.request.contextPath}/ProductServlet?method=findByCid&cid=" + cat.cid + "'>" + cat.cname + "</a>
========ContextPath:/day51_store68Self
request.getRequestURL():获得完整请求路径名(注意不包括请求参数,如果带参数的话);
request.getRequestURI():只包括项目名及其后面的的 URL部分(也就是web资源路径);
已测实例:
========RequestURI:/day51_store68Self/IndexServlet
========RequestURL:http://localhost:8080/day51_store68Self/IndexServlet
request.getQueryString():问号后面内容。
已测实例:
<a href='${pageContext.request.contextPath}/ProductServlet?method=findByCid&cid=" + cat.cid + "'>" + cat.cname + "</a>
========QueryString:method=findByCid&cid=1
====【2018/4/9 知识补充】:
<%@ page contentType="text/html;charset=GBk" import="java.util.*"%>
<html>
<head>
<title>请求信息</title>
</head>
<body>
<p>使用request对象的方法获取信息</p>
<%
//服务器
String localName=request.getLocalName();
String serverName = request.getServerName();
String localAddr=request.getLocalAddr();
int localPort=request.getLocalPort();
int serverPort = request.getServerPort();%>
<b>服务器</b>:<%= localName %><br/>
<b>服务器端IP</b>:<%= localAddr %><br/>
<b>服务器端口</b>:<%= localPort %><p/>
//客户端信息
String remoteHost=request.getRemoteHost();
String remoteAddr=request.getRemoteAddr();
int remotePort=request.getRemotePort();%>
<b>浏览器端</b>:<%= remoteHost %><br/>
<b>浏览器端IP是</b>:<%= remoteAddr %><br/>
<b>浏览器端口</b>:<%= remotePort %><p/>
<%
//协议相关
String pro=request.getProtocol();
String pro1=request.getScheme();
int len=request.getContentLength();
String type=request.getContentType();
String charEncode=request.getCharacterEncoding();
%>
<b>协议版本</b>:<%= pro %><br/>
<b>协议</b>:<%= pro1 %><br/>
<b>数据内容长度</b>:<%= len %><br/>
<b>数据类型</b>:<%= type %><br/>
<b>字符编码方式</b>:<%= charEncode %><p/>
</body>
</html>