1、首先在当前界面获取URL信息:
public java.lang.StringBuffer getRequestURL()
getRequestURL()会得到一个完整的URL地址,也就是绝对的绝对地址。如:http://localhost:8080/MyProject/Test/NewTask.jsp,返回值为StringBuffer型,通过.tostring()方法转化为String型。
public java.lang.String getRequestURI()
getRequestURI()就相当于你在写一个JSP页面的时候会有这样的东西"action='/MyProject/xxx'"这个方法就是获得'/MyProject/xxx',也就是说它会得到一个相对地址。如:/MyProject/Test/NewTask.jsp
getServletPath(), 获取所请求的文件路径,即工程名后面的路径。/Test/NewTask.jsp
getRemoteAddr(),获取客户端IP地址.
getQueryString()得到地址中传递的参数,即?后面的内容。
因此,获取整个地址的方法如下:
HttpServletRequest httprequest = (HttpServletRequest) request; String path1=httprequest.getRequestURL().toString(); String path2=httprequest.getQueryString(); String currpath=path1+"?"+path2;
在当前Jsp界面利用一个隐藏的文本框保存该值,然后就可以在JS中获取该值:
<input type="hidden" name="currentpath" id="currentpath" value="<%=currpath%>"/>
2、在JS中实现点击按钮复制链接
function Copy() { var txtObj=document.getElementById("currentpath"); var text=txtObj.value; window.clipboardData.setData("Text",text); alert("已经复制到剪贴板!"); //var clipValue=window.clipboardData.getData("text");//text is not case sensitive //alert(clipValue); }
3、按钮
<input type="button" value="Copy URL" onclick="Copy()">