範例程式碼:JSP檔案 <% @ page contentType = " text/html; charset=gb2312 " %>
< html >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=gb2312 " >
< title > 先彈出提示資訊,之後點擊確定再跳轉向新的地址 </ title >
</ head >
< body >
<%
String url = request.getParameter( " url " );
String hasdays = request.getParameter( " hasdays " );
%>
< script type = " text/javascript " >
alert( " 系統提示:您的賬戶馬上就要到期了][剩餘 " +<%= hasdays %>+ " 天 " );
window.location.href = " <%=url%> " ;
</ script >
系統提示:您的賬戶馬上就要到期了 !! 剩餘 <%= hasdays %> 天;
<%
response.sendRedirect(url);
%>
</ body >
</ html >
本來是希望上述代碼實現:先提示將要到期的資訊,之後再點擊確定才跳轉向response.sendRedirect指向的地址
可是由於jsp中嵌入的java代碼是在伺服器端就執行了的,所以上述代碼的執行效果並不能滿足既定的功能設想,而
是不會出現彈出資訊,直接就執行了response.sendRedirect!
為了實現上述的功能設想我們只有利用JavaScript來代替response.sendRedirect完成地址的跳轉,代碼如下:
<% @ page contentType = " text/html; charset=gb2312 " %>
< html >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=gb2312 " >
< title > 先彈出提示資訊,之後點擊確定再跳轉向新的地址 </ title >
</ head >
< body >
<%
String url = request.getParameter( " url " );
String hasdays = request.getParameter( " hasdays " );
%>
< script type = " text/javascript " >
alert( " 系統提示:您的賬戶馬上就要到期了][剩餘 " +<%= hasdays %>+ " 天 " );
window.location.href = " <%=url%> " ; <!-- JavaScript代替response.sendRedirect做跳轉 -->
</ script >
系統提示:您的賬戶馬上就要到期了,剩餘 <%= hasdays %> 天;
</ body >
</ html >
OK!! 問題解決了!!