Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别
1 区别
假定你的web application 名称为news,你在浏览器中输入请求路径:
http://localhost:8080/news/main/list.jsp
1.1 System.out.println(request.getContextPath());
打印结果:/news
1.2 System.out.println(request.getServletPath());
打印结果:/main/list.jsp
1.3 System.out.println(request.getRequestURI());
打印结果:/news/main/list.jsp
1.4 System.out.println(request.getRealPath("/"));
打印结果:F:\Tomcat 6.0\webapps\news\test
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path
这其实就是 获得应用的根url,比如说你的应用的根路径是 http://localhost:8080,那么你列出的代码就是为basePath赋值为 http://localhost:8080。
具体点:
1、request.getScheme() 返回协议的名称 http,和后面的"://" 拼起来就成了 http://
2、request.getServerName() 这是获取你的服务器的名称,如果你的应用部署在本机那么其就返回localhost或者127.0.0.1 ,这2个是等价的
3、request.getServerPort() 是你应用使用的端口,比如8080或者80 等等
Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别的更多相关文章
- request的getServletPath(),getContextPath(),getRequestURI(),getRealPath("/")区别
假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...
- Servlet的getContextPath(), getServletPath(), getRequestURI(), getRealPath("/")
假定web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果: ...
- getContextPath、getServletPath、getRequestURI,getRealPath的区别
假定你的web application 项目名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下 ...
- SoapUI利用Groovy把外部数据加载到request中
默认已经用Groovy把外部数据给读取出来了,关键是读取出来后,如何加载到request中去?这里提供了两种方法:1.该Groovy脚本的名称是"setUp" def num = ...
- (转)关于request.getServletPath(),request.getContextPath()的总结
文章完全转载自 : https://blog.csdn.net/qq_27770257/article/details/79438987 最近对于request中的几种“路径”有点混淆,查找网上资源都 ...
- getContextPath、getServletPath、getRequestURI、getRealPath、getRequestURL、getPathInfo();的区别
<% out.println("getContextPath: "+request.getContextPath()+"<br/>"); ou ...
- jsp中的<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+ ...
- 修改Request 中的数据
拦截器修改参数 今天一位网友开发中遇到一个需求,他需要在Request中修改传递过来的数据.开始的时候他在拦截器中修改,在拦截器中可以获取到从前台request中 传递过来的数据.他写法大致如下:自定 ...
- 从request获取各种路径总结 request.getRealPath("url")
转载:http://blog.csdn.net/piaoxuan1987/article/details/8541839 equest.getRealPath() 这个方法已经不推荐使用了,代替方法是 ...
随机推荐
- php数据几行代码导出到excel(非插件)
<?php header("Content-type:application/vnd.ms-excel"); header("Content-Disposition ...
- Git 对已经加入版本控制的文件,修改后希望不被提交办法
参考网址:http://my.oschina.net/zlLeaf/blog/197740 问题举例:假设网站有一个数据库配置文件db.php,通过git做版本控制,已经将这个文件提交到git库中.但 ...
- webapp-viewport 相关知识整理
我们在开发移动设备的网站时,最常见的的一个动作就是把下面这个东西复制到我们的head标签中: <meta name="viewport" content="widt ...
- warning: deprecated conversion from string constant to 'char*
warning: deprecated conversion from string constant to 'char* #include<iostream> using namespa ...
- 本地项目上传git@osc
本地项目使用eclipse的git插件上传到开元中国社区的代码托管平台 1.在托管平台新建项目 2.使用eclipse的git repositories ,在URI中输入新建项目的https路径; 验 ...
- java-日期类
一 显示系统时间 package cn.itcast.api.a.date; import java.text.DateFormat; import java.util.Date; public cl ...
- Codeforces 851D Arpa and a list of numbers
D. Arpa and a list of numbers time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Total Number of Unicorn Companies: 188
https://www.cbinsights.com/research-unicorn-companies Total Number of Unicorn Companies: 188 Total ...
- visual studio code 调试reactjs
1.首先到visual studio code官网下载ide. 2.打开visual studio code,点击右侧菜单条的小图标 找到[Debugger for Chrome],并安装 3.打开c ...
- 三、Redis操作
一.基本介绍 1.简介 Redis是由意大利人Salvatore Sanfilippo(网名:antirez)开发的一款内存高速缓存数据库.Redis全称为:Remote Dictionary Ser ...