String path = request.getContextPath(); 
String basePath = request.getScheme()
+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
    <base href=" <%=basePath%>"> 

这个语句是用来拼装当前网页的相对路径的。

<base href="...">是用来表明当前页面的相对路径所使用的根路径的。 
比如,页面内部有一个连接,完整的路径应该是 http://localhost:80/myblog/authen/login.do 
其中http://
server/是服务器的基本路径,myblog是当前应用程序的名字,那么,我的根路径应该是那么http://localhost:80/myblog/。

有了这个 <base ... >以后,我的页面内容的连接,我不想写全路径,我只要写 authen/login.do就可以了。服务器会自动把 <base ...>指定的路径和页面内的相对路径拼装起来,组成完整路径。 
如果没有这个 <base...>,那么我页面的连链接就必须写全路径,否则服务器会找不到。

request.getSchema()可以返回当前页面使用的协议,就是上面例子中的“http” 
request.getServerName()可以返回当前页面所在的服务器的名字,就是上面例子中的“localhost" 
request.getServerPort()可以返回当前页面所在的服务器使用的端口,就是80, 
request.getContextPath()可以返回当前页面所在的应用的名字,就是上面例子中的myblog 
这四个拼装起来,就是当前应用的跟路径了

最近在自学struts,被里面struts中action 的path与form表单的action属性弄迷糊了。

struts-config.xml 文件中,action元素中的path属性表示的是浏览器地址栏中相对于应用程序根目录的请求路径,与form 中提交表单以后有谁处理的action属性指定的根路径一致。(只是一致,千万不要以为是绝对相等~)

例如:form表单的提交处理请求是classesAdd.do,其在ie地址栏中的路径如下所示,

http://localhost:9000/Struts_study/classesMan/classesAdd.do

红色部分表示的根路径,所以,action中的classesAdd.do请求的完整路径是classesMan/classesAdd.do

<form name="form1" action="classesAdd.do" method=post>

所以 struts 中的action 的path路径是指/classesMan/classesAdd。

大家可以看着浏览器的地址栏加以配置。祝大家晚上睡觉快乐。

 

补充一下吧,form中的action的默认路径就是当前路径,

而struts中的action 的path属性默认路径为根路径,所以要加上所在的文件夹得路径。

使用方法如:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>404</title>
<meta http-equiv="refresh" content="60;url=index.jsp">
<!-- content="600,即600秒后返回主页,可根据需要修改或者删除这段代码 -->
<link href="css/error.css" rel="stylesheet" type="text/css" />
</head>
<body>
<!-- 代码 开始 -->
<div id="container"><img class="png" src="data:images/404.png" /> <img class="png msg" src="data:images/404_msg.png" />
<p><a href="index.jsp"><img class="png" src="data:images/404_to_index.png" /></a> </p>
</div>
<div id="cloud" class="png"></div>
<!-- 代码 结束 --> </body>
</html>

JSP中的:request.getScheme()+"://"+request.getServerName()+":"+request.getServer的更多相关文章

  1. JSP中字符编码转换问题

    问题描述:一个input.jsp页面中的参数,传递到另外一个save.jsp页面上,然后存入到数据库中,如果input.jsp页面输入偶数中文没有问题,输入奇数则出现?,存入数据库的也是?. 问题源码 ...

  2. jsp中的<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

    <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+ ...

  3. jsp页面String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";作用!!!!!

    转自:https://blog.csdn.net/kiwangruikyo/article/details/81130311 <%String path = request.getContext ...

  4. <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

    <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+re ...

  5. jsp中的request.getContextPath()

    jsp中的request.getContextPath()   <%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径. 但不用也可以吧,比 ...

  6. JSP 中的 Request 和 Response 对象

    客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例:response对象包含了响应客户请求的有关信息,但在JSP中 ...

  7. 关于String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

    关于 String path = request.getContextPath(); String basePath = request.getScheme()+"://"+req ...

  8. String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";作用!!!!!

    <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+re ...

  9. basePath = request.getScheme()+"://"+request.getServerName()+":"+r

    basePath = request.getScheme()+"://"+request.getServerName()+":"+r (2014-06-30 1 ...

随机推荐

  1. Python分布式爬虫抓取知乎用户信息并进行数据分析

    在以前的文章中,我写过一篇使用selenium来模拟登录知乎的文章,然后在很长一段时间里都没有然后了... 不过在最近,我突然觉得,既然已经模拟登录到了知乎了,为什么不继续玩玩呢?所以就创了一个项目, ...

  2. JS简写

    本文来源于多年的 JavaScript 编码技术经验,适合所有正在使用 JavaScript 编程的开发人员阅读. 本文的目的在于帮助大家更加熟练的运用 JavaScript 语言来进行开发工作. 文 ...

  3. The Road to learn React书籍学习笔记(第一章)

    react灵活的生态圈 Small Application Boilerplate: create-react-app Utility: JavaScript ES6 and beyond Styli ...

  4. PHP.28-TP框架商城应用实例-后台5-多表操作-商品表与品牌表

    表与表之间的关系:1:1 1:多 多:多 功能需求决定表关系 此处的表关系为:品牌表:商品表=1:多 1.首先在表结构上关联,在多的表(商品表)添加一个字段,关联一的表(品牌表)的ID(主键) 添加字 ...

  5. 安测云验证有CTA问题

    背景: 现在所有的app 都需要通过工信部的审核.用户不同意之前,不能联网. 那么,我怎么知道自己的应用有没有联网呢?那么多sdk ,那么多代码?我怎么测试呢? 哈哈,我们测试给的方法真的很管用. l ...

  6. Pycharm设置Python的路径

    1. 打开文件->默认设置 2. 找到Python的路径即可,如果没有的话,这里也可以安装一个,只是时间比较久. 3. 选择本地 4. 选择文件

  7. 【Feasibility of Learning】林轩田机器学习基石

    这一节的核心内容在于如何由hoeffding不等式 关联到机器学习的可行性. 这个PAC很形象又准确,描述了“当前的可能性大概是正确的”,即某个概率的上届. hoeffding在机器学习上的关联就是: ...

  8. Oracle 插入数据时获取系统时间

    insert into table_xx (xx,dateTime) values( 'xx',sysdate) 这个sysdate 相当于sql server中的GETDATE()函数 另to_ch ...

  9. sql 游标使用

    declare @PASSDate datetime,@VLPN varchar(50),@VLPNColor varchar(10),@nambers int set @VLPN='';set @V ...

  10. python pip install XXX出现报错问题

    重装Anacondas后,将pip 和python.exe路径加入到环境变量后直接在cmd窗口进行pip 操作,报错如下 报错内容为: pip is configured with locations ...