記憶登錄跳轉可以打打方便用戶使用getHeader("Referer");方式簡單不過不能處理post的參數本來都是用在struts,webwork裡的,為了容易看明白改寫成jsp方式的
-----------------------------------------------------------------
member.jsp //session限制訪問頁
<%
String url = ""
if(session.getAttribute("username")==null)
{
url = myRedirect.dealurl(request);//記錄當前地址和請求參數,dealurl將實際url處理了避免和要請求url有干擾,因為會有& 字符
response.sendRedirect("weblogin.jsp?url="+url) ;//重定向到登錄頁面
}
%>
------------------------------------------------
weblogin.jsp
<%
String url = request.getParameter("url");
String userid =request.getParameter("userid");
String password =request.getParameter("password");
if(userid!=null) //如果是登錄提交執行下面代碼
{
if(登錄成功)
{
session.setAttribute("userid",userid);
out.println("<script>alert(登錄成功,謝謝光臨);self.location.href="+myRedirect.geturl(url)+";</script>");//轉向到首次請求的url,.geturl(url)是把轉化過的地址轉化回來變成真實url.
}else{
out.println("<script>alert(用戶名或密碼錯誤);history.back();</script>");
}
}else{//如果不是登錄提交顯示現面登錄界面
%>
<table width="311" height="162" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="311" height="162" align="center"> 您還沒有登錄,請先登錄<br>
<table width="155" border="0" cellpadding="0" cellspacing="5" class="font12">
<form name="loginfrm" action="weblogin.jsp" method="post" id="loginfrm" onSubmit="return checkfrm();">
<tr>
<td width="41" height="33" valign="bottom">帳 號:</td>
<td width="99" valign="bottom"><input name="userid" type="text" class="textstyle" id="userid" size="12" ></td>
</tr>
<tr>
<td>密 碼:</td>
<td><input name="password" type="password" class="textstyle" id="password" size="12" ></td>
</tr>
<tr align="center" valign="bottom">
<td height="21" colspan="2"> <input type="submit" name="Submit" value="登錄">
<a href="reg.jsp" target="_blank">沒有注冊</a> </td>
</tr>
<tr align="center">
<td colspan="2" class="font14"><strong><a href="#" onclick="MM_openBrWindow(requestpwd.jsp,pwd,width=300,height=200);return false;">找回密碼</a></strong></td>
</tr>
<input name="url" type="hidden" value="<%=url%>">這裡要有個hidden來保存一下,不然提交就沒了
</form>
</table>
</td>
</tr>
</table>
<%
}
%>
---------------------------------------------------------------
myRedirect.java
package com.util;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
public class myRedirect //對url的轉換類
{
public static String dealurl(HttpServletRequest request)
{
String url = "";
url = request.getRequestURL()+"?";
url +=param(request);
if(url.indexOf("&")>-1)
url=url.replaceAll("&","@#@");//實際上就是把有&的字符轉化成了@#@
return url;
}
public static String geturl(String url)//這個是還原方法
{
if(url.indexOf("@#@")>-1)
url=url.replaceAll("@#@","&");
return url;
}
public static String param(HttpServletRequest request)
{
String url = "";
Enumeration param = request.getParameterNames();//得到所有參數名
while(param.hasMoreElements())
{
String pname = param.nextElement().toString();
url += pname+"="+request.getParameter(pname)+"&";
}
if(url.endsWith("&"))
{
url = url.substring(0,url.lastIndexOf("&"));
}
return url;
}
}
這樣登錄浏覽器就會自動轉到用戶剛剛請求的地址