js记忆登录跳转可以打打方便用户使用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.width="311" height="162" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="311" height="162" align="center"> 您还没有登录,请先登录
<table width="155" border="0" cellpadding="0" cellspacing="5" class="font12">
<form name="loginfrm" action="weblogin.jsp" method="post" id="loginfrm" >
<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" </tr>
<tr>
<td>密 码:</td>
<td><input name="password" type="password" class="textstyle" id="password" size="12" </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="#" >找回密码</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;
}
}
这样登录浏览器就会自动转到用户刚刚请求的地址