WEB基础:几个和路径有关的方法

来源:互联网 发布:税务软件安装 编辑:程序博客网 时间:2024/06/04 19:38

   在编写JSP/Servlet应用时常常需要从传入的链接中提取一些信息。HttpServletRequest提供了多个方法,这些方法之间容易混淆。总结并举例如下,希望能对大家有所帮助。    
  举例:  
  http://localhost:7001/myservlet/somepath/test?someparam=somevalue    
  request.getPathInfo():返回/somepath/test    
  request.getRequestURL():返回http://localhost:7001/myservlet/somepath/test    
  request.getRequestURI():返回/myservlet/somepath/test    
  request.getServletPath():返回/myservlet    
  request.getQueryString():返回someparam=somevalue 

另外:

  给出一个获取系统路径的JSP页:

<%@page
 language="java"
 contentType="text/html;charset=gb2312"
%>
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<%@page import="javax.servlet.http.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<%
String sysypath=application.getRealPath("");

ServletContext servletContext = config.getServletContext();
String rootPath = servletContext.getRealPath("/");

String sys=System.getProperty("user.dir");

String ddd=(String)request.getRequestURL().toString();

String   uri   =   request.getRequestURI();
%>
<body>
<div align="center">
  <p>系统路径 <%=sysypath%> </p>
  <p>系统路径 <%=rootPath%> </p>
  <p>sys<%=sys%> </p>
  <p>url<%=ddd%> </p>
  <p>uri<%=uri%> </p>
</div>
</body>
</html>

 

实践使你学习的更快!

原创粉丝点击