记忆登录跳转可以打打方便用户使用
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" style="height:20"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input name="password" type="password" class="textstyle" id="password" size="12" style="height:20;width:94"></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;
}
}
这样登录浏览器就会自动转到用户刚刚请求的地址