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编程从入门到实践#第三章笔记

      列表简介 ​​​1.什么是列表 列表:由一系列按也顶顺序排列的元素组成.元素之间可以没有任何关系. 列表:用方括号[]表示,并用逗号分隔其中元素.名称一般为复数 2.访问元素 (1)列表是有序集合 ...

  2. Highest Tower 18中南多校第一场H题

    一.题意 给出N个方块,要求给出一个方案,使得1. 所有方块都被使用到(题目数据保证这点) 2.所有方块垒成一个塔,且上面的方块宽度小于下面的方块 3.每个方块只能用一次,可以横着或者竖着. n范围5 ...

  3. PHP.14-图片处理类

    图片处理类 test.php <?php include "images.class.php"; $image=new Image("./images/" ...

  4. Servlet过滤器---登录权限控制

    实现了登录时权限控制:进入首页.登录页以及登录servlet时,不用验证权限:进入其它页面时,须验证是否登录,未登录则跳转到登录页. 一个简单的首页:index.jsp <%@ page lan ...

  5. hover 改变另一个标签的属性

  6. Python数据类型三

    一.帮助 如果想知道一个对象(object)更多的信息,那么可以调用help(object)!另外还有一些有用的方法,dir(object)会显示该对象的大部分相关属性名,还有object._ doc ...

  7. elasticsearch安装教程

    目录 1 java8 环境 2 安装elasticsearch 3 安装kibana 4. 单服务器部署多个节点 参考: 1 java8 环境 elasticsearch需要安装java 8 环境,配 ...

  8. angular 模块化之directive

    通过使用directive使页面模块化.需要哪部分直接调用即可.原本这些操作需要后端配合,现在前端即可.将原本的html代码拆为不同的模块,然后通过directive衔接加载到主页面中.首先页面通过d ...

  9. js获取可编辑区域光标位置

    请到简书中看,地址: http://www.jianshu.com/p/19a507cd5fd7 github测试例子 https://github.com/Stevenzwzhai/plugs/tr ...

  10. node express 登录拦截器 request接口请求

    1.拦截器 拦截器可以根据需要 做权限拦截 登录只是权限的一种, 思路是req.session.user判断用户session是否存在,是否是需要拦截的地址, 如果是就跳转登录页,或其他页, 如果非需 ...