码迷,mamicode.com
首页 > Web开发 > 详细

JSP中的:request.getScheme()+"://"+request.getServerName()+":"+request.getServer

时间:2017-09-09 17:17:26      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:请求   content   base   eth   pop   开始   body   enc   port   

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="images/404.png" /> <img class="png msg" src="images/404_msg.png" />
  <p><a href="index.jsp"><img class="png" src="images/404_to_index.png" /></a> </p>
</div>
<div id="cloud" class="png"></div>
<!-- 代码 结束 -->

 
</body>
</html>

 

 

JSP中的:request.getScheme()+"://"+request.getServerName()+":"+request.getServer

标签:请求   content   base   eth   pop   开始   body   enc   port   

原文地址:http://www.cnblogs.com/qlqwjy/p/7498522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!