EL表达式获取地址栏地址以及jsp如何获取服务器信息



有时候我们需要在jsp页面获取当前地址。(非js获取)


获取浏览器地址栏输入的地址

EL表达式获取

 ${requestScope['javax.servlet.forward.request_uri']}
或者

${pageContext.request.getAttribute("javax.servlet.forward.request_uri")}




java脚本获取

<% 
String url=request.getAttribute("javax.servlet.forward.request_uri").toString();

%>
<%=url%>



ps注意事项

Tomcat和Was7对servlet标准的实现不尽相同的。差异很大,导致最终结果也有些差异了。本文中要说的request.getAttribute("javax.servlet.forward.request_uri")调用结果就是其中的一个差异。
在Tomcat环境下javax.servlet.forward.request_uri存放的是当前请求路径,但在was7下存放的却是上一个请求的路径。





获取浏览器地址栏输入地址的请求参数

例如:http://localhost:8080/test/hello?name=joe获取name

EL表达式获取

${param.name}


java脚本获取

<% 
String name = request.getParameter("name");
%> 
<%=name%> 






获取当前页面绝对路径

java脚本

<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
String  url  =  "http://"  +  request.getServerName()  +  ":"  +  request.getServerPort()  +  request.getContextPath()+request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/")+1);  

if(request.getQueryString()!=null) 
{   
    url+="?"+request.getQueryString();           
} 
System.out.println("path:"+path); 
System.out.println("basePath:"+basePath);    
System.out.println("URL:"+url);   

System.out.println("URL参数:"+request.getQueryString());  
%>
或者

String   url=request.getScheme()+"://";   
  url+=request.getHeader("host");   
  url+=request.getRequestURI();   
  if(request.getQueryString()!=null)   
      url+="?"+request.getQueryString(); 


EL表达式

${pageContext.request.requestURL}




获取服务器的相关信息

EL表达式

${pageContext.request}                  |取得请求对象<br>  
${pageContext.session}                  |取得session对象<br>  
${pageContext.request.queryString}      |取得请求的参数字符串<br>  
${pageContext.request.requestURL}       |取得请求的URL的服务器路径,但不包括请求之参数字符串<br>  
${pageContext.request.contextPath}      |服务的web application的名称<br>  
${pageContext.request.method}           |取得HTTP的方法(GET、POST)<br>  
${pageContext.request.protocol}         |取得使用的协议(HTTP/1.1、HTTP/1.0)<br>  
${pageContext.request.remoteUser}       |取得用户名称<br>  
${pageContext.session.new}              |判断session是否为新的,所谓新的session,表示刚由server产生而client尚未使用<br>  
${pageContext.session.id}               |取得session的ID<br>  
  
${header["User-Agent"]}|用户浏览器的版本<br/>  
${header["Host"]}|IP<br/>  
${pageContext.request.remoteAddr }      |取得用户的IP地址<br>  
${pageContext.servletContext.serverInfo}|取得主机端的服务信息<br>  
${pageContext.request.serverPort}|端口信息<br>  
${pageContext.request.serverName}|服务器名称<br>  
${pageContext.request.remoteHost}|客户机名称<br>  


java脚本

<%  
  
String SERVER_NAME = request.getServerName();  
  
String SERVER_SOFTWARE = getServletContext().getServerInfo();  
  
String SERVER_PROTOCOL = request.getProtocol();  
  
Integer SERVER_PORT = request.getServerPort();  
  
String REQUEST_METHOD = request.getMethod();  
  
String PATH_INFO = request.getPathInfo();  
  
String PATH_TRANSLATED = request.getPathTranslated();  
  
String SCRIPT_NAME = request.getServletPath();  
  
String DOCUMENT_ROOT = request.getRealPath("/");  
  
String QUERY_STRING = request.getQueryString();  
  
String REMOTE_HOST = request.getRemoteHost();  
  
String REMOTE_ADDR = request.getRemoteAddr();  
  
String AUTH_TYPE = request.getAuthType();  
  
String REMOTE_USER = request.getRemoteUser();  
  
String CONTENT_TYPE = request.getContentType();  
  
Integer CONTENT_LENGTH = request.getContentLength();  
  
String HTTP_ACCEPT = request.getHeader("Accept");  
  
String HTTP_USER_AGENT = request.getHeader("User-Agent");  
  
String HTTP_REFERER = request.getHeader("Referer");  

%>





你可能感兴趣的:(jsp,EL,服务器,url,获得地址)