<1> 获取用户的真实IP地址
//如果使用了代理,必须这样。
String realIP = request.getHeader("x-forwarded-for");
String ip = request.getRemoteAddr();
if(realIP==null||realIP.length()==0){
out.println("你的IP地址是:"+ip);
}else{
out.println("你的IP地址是:"+realIP);
}
<2> JSP中获取当前绝对路径
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Untitled Document</title>
</head>
<body>
当前WEB应用的物理路径:<%=application.getRealPath("/")%><BR>
当前你求请的JSP文件的物理路径:<%=application.getRealPath(request.getRequestURI())%><BR>
<%
String path=application.getRealPath(request.getRequestURI());
String dir=new File(path).getParent();
out.println("当前JSP文件所在目录的物理路径"+dir);
%>
</body>
</html>
<3> JSP获取路径与URL
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Untitled Document</title>
</head>
<body>
当前WEB应用的物理路径:<%=application.getRealPath("/")%><BR>
当前你求请的JSP文件的物理路径:<%=application.getRealPath(request.getRequestURI())%><BR>
<%
String path=application.getRealPath(request.getRequestURI());
String dir=new File(path).getParent();
out.println("当前JSP文件所在目录的物理路径"+dir+"</br>");
String realPath1 = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath()+request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/")+1);
out.println("web URL 路径:"+realPath1);
%>
</body>
</html>