`

getContextPath、getServletPath、getRequestURI的区别(转)

 
阅读更多

转自:http://veryls.iteye.com/blog/239830 

假定你的web application 名称为news,你在浏览器中输入请求路径: 
http://localhost:8080/news/main/list.jsp 
则执行下面向行代码后打印出如下结果: 
1、 System.out.println(request.getContextPath()); //可返回站点的根路径。也就是项目的名字 
打印结果:/news 
   2、System.out.println(request.getServletPath()); 
打印结果:/main/list.jsp 
3、 System.out.println(request.getRequestURI()); 
打印结果:/news/main/list.jsp 
4、 System.out.println(request.getRealPath("/")); 
打印结果:F:\Tomcat 6.0\webapps\news\test 

分享到:
评论

相关推荐

    Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别.doc

    Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别

    Request中getContextPath、getServletPath的区别

    NULL 博文链接:https://javachikuang.iteye.com/blog/262264

    request.getcontextPath()_详解

    request.getcontextPath()详解

    java中request.getcontextPath()的意思.doc

    java中request.getcontextPath()的意思.doc

    WEB15-request.rar

    String getRequestURI() StringBuffer getRequestURL() String getContextPath() ---web应用的名称 String getQueryString() ---- get提交url地址后的参数字符串 username=zhangsan&password=123 注意:request...

    数据库测试test.sql

    response.sendRedirect(request.getContextPath()+"/admin/login.jsp"); } public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //...

    新闻推荐系统

    所有的jsp页面中的链接 添加 request.getContextPath() , 确保路径正确使用!! 代码中使用到了重定向等页面跳转,添加 request.getContextPath() , 确保路径正确使用!! dao 层中的接口不要随意修改,如果需要...

    Android代码-YuanNews

    所有的jsp页面中的链接 添加 request.getContextPath() , 确保路径正确使用!! 代码中使用到了重定向等页面跳转,添加 request.getContextPath() , 确保路径正确使用!! dao 层中的接口不要随意修改,如果需要...

    解决'JSON'未定义 报错,引入json2.js

    将JavaScript对象类型的参数通过JSON.stringify转换成字符串传递时,IE5、6、7、8会报:“JSON”未定义 的...解决方法:路径/json2.js">或者<script src="<%=request.getContextPath()%>/passripts/json2.js"></script>

    网页视频播放插件

    var so = new SWFObject("<%=request.getContextPath()%>/js/player/CuPlayerMiniV4.swf","CuPlayerV4","600","410","9","#000000"); so.addParam("allowfullscreen","true"); so.addParam("allowscriptaccess",...

    DWR开发包常规客户端服务器端面向对象编程

    dwr开发包,常规客户端...<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/TestService.js'> <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'> ...

    json字符串之间的相互转换示例代码

    通过eval()函数可以将JSON字符串转化...String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOC

    My97DatePicker日期控件项目应用

    My97DatePicker日期控件使用引入js脚本目录,注意必须用<%=request.getContextPath() %>来获取项目根目录+脚本目录的方式,否则日期控件会无效!!! <script type="text/javascript" src="<%=request....

    JSP之表单提交get和post的区别详解及实例

    一 get和post的区别 二 实战(post方式提交) 1、login.jsp <%@ page language=java import=java.util.* contentType=text/html; charset=utf-8 %> <% String path = request.getContextPath(); ...

    网页浮动窗口

    <script type="text/javascript" src="<%=request.getContextPath()%>//res/js/float.js"> var div1= new floatFrame( "id", "style", "title", "content", speed, startPositionX, startPositionX, ...

    jQuery实现的点击按钮改变样式功能示例

    分享给大家供大家参考,具体如下: 1. css代码:index4.css @CHARSET "UTF-8"; .sd{ font-weight: bold; color: black;... background: red;...Stringpath = request.getContextPath(); StringbasePath = reque

    最简单js代码实现select二级联动下拉菜单

    String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD H

    JSP实现简单的用户登录并显示出用户信息的方法

    本文实例讲述了JSP实现简单的用户登录并...String path = request.getContextPath();  String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;  %> 

    Java后端实现websocket与微信小程序端连接简单例子

    String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>   <!DOCTYPE HTML> <html>  <head> ...

    JFreeChart报表及其实例

    String graphURL =request.getContextPath+"/servlet/DisplayChart?filename="+filename;生成filename路径,而我运行不出来,而是根据文档 变成ChartUtilities.writeChartAsJPEG(response.getOutputStream(),chart,...

Global site tag (gtag.js) - Google Analytics