URL末尾加“/”與不加“/"的區別
由於兩個系統間要進行頁面跳轉,並且需要前系統往後系統傳值。於是,在前系統中,我用post的方式往後系統傳值,在後系統用了request.getParameter("")來得到前系統傳過來的值。
前系統程式碼:
...
<div id="yDoChinaLogin" style="float:right; width:70%;"">
<div class="rightTopDiv">
<c:choose>
<c:when test="${resultModel.loginMode eq
'incognito'}">
<div
style="height:30px;"></div>
</c:when>
<c:otherwise>
<div class="rightDiv" style="height:30px;">
<c:forEach var="menu" items="${resultModel.topMenuList}" step="1"
varStatus="menuStatus">
<div
class="rightSysDiv" style="float:left; width:170px;">
<span class="rightSysHref" title="${menu.url}"
id="${menu.systemId},${menu.safeLoginFlag}"
onclick="onSystemLogin(this.title,this.id)" ><u>${menu.systemName}</u>
</span>
</div>
</c:forEach>
</div>
</c:otherwise>
</c:choose>
</div>
...
<h:form styleId="LoginForm" method="POST" action="/hello/Welcome"
onsubmit="return false" target="_self">
<h:hidden
property="userContext.userId" value="${userContext.userId}" />
<h:hidden property="userContext.userName" value="${userContext.userName}"
/>
<h:hidden property="flag" />
<h:hidden
property="systemId" />
</h:form>
<script
type="text/javascript">
function onSystemLogin(actionPath, sysIdAndFlag)
{
var array = new Array();
array = sysIdAndFlag.split(",");
document.getElementById("systemId").value = array[0];
document.getElementById("flag").value = array[1];
var form =
document.forms["LoginForm"];
<h:msgSelfPageSet
key="ymc-commons.P.00004" />
form.action = actionPath;
form.target = "_bank";
form.submit();
}
後系統JSP端程式碼:
<%
String
flag = request.getParameter("flag");
if (flag != null) {
if (flag.equals("false")) {
%>
<jsp:forward
page="/login/Xb03TopLogin.do"/>
<%
} else {
%>
<jsp:forward page="/login/LoginTop.do"/>
<%
}
} else {
%>
<jsp:forward
page="/login/LoginTop.do"/>
<%
}
%>
在資料庫中,定義url為http://localhost:8080/ymsm_mdm_war
一切編譯OK通過,啟動,點選連結,發現後系統接收不到前系統傳過來的值,值都為null。很奇怪!
經查詢,發現是傳遞的url末尾加”/“與不加”/“問題。
不加"/",系統會自動進行301的重定向,把url變成末尾加”/“的地址。而request.getParameter("")僅有一次生命週期,經過兩次跳轉後,前系統傳的值失效了。此外,如直接傳送 URL:http://msdn.microsoft.com/ie/ 比傳送http://msdn.microsoft.com/ie速度將會更快,當然這種速度是感覺不到的,但優化應該“儘可能”,減少浪費的時間。
關於,301重定向問題,可參考http://blogs.msdn.com/ie/archive/2005/04/11/407189.aspx