Memory login jump can be typed for user convenience
getHeader("Referer"); The method is simple but cannot handle the parameters of post. They were originally used in struts and webwork. In order to make it easier to understand, it was rewritten into jsp method
-------------- -------------------------------------------------- --------------------------------------------------
member.jsp //session restricted access page
<%
String url = ""
if(session.getAttribute("username")==null)
{
url = myRedirect.dealurl(request);//Record the current address and request parameters, dealurl processes the actual url to avoid interference with the requested url, because there will be & characters
response.sendRedirect("weblogin.jsp?url="+url);//Redirect to the login page
}
%>
-------------------------------------------------- ----------------------------------------
weblogin.jsp
<%
String url = request.getParameter("url");
String userid =request.getParameter("userid");
String password =request.getParameter("password");
if(userid!=null) //If you are logging in and submitting, execute the following code
{
if (login successful)
{
session.setAttribute("userid",userid);
out.println("<script>alert('Login successful, thank you for visiting');self.location.href='"+myRedirect.geturl(url)+"';</script>");//Redirect to the first time The requested url, .geturl(url) converts the converted address back into the real url.
}else{
out.println("<script>alert('Username or password is incorrect');history.back();</script>");
}
}else{//If it is not a login submission, the current login interface will be displayed.
%>
<table width="311" height="162" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="311" height="162" align="center"> You are not logged in yet, please log in first<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">Account number:</td>
<td width="99" valign="bottom"><input name="userid" type="text" class="textstyle" id="userid" size="12" style="height:20"></ td>
</tr>
<tr>
<td>Password:</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="Login">
<a href="reg.jsp" target="_blank">Not registered</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;">Retrieve password</a></strong></td>
</tr>
<input name="url" type="hidden" value="<%=url%>">There needs to be a hidden here to save it, otherwise the submission will be lost.
</form>
</table>
</td>
</tr>
</table>
<%
}
%>
------------------------------------------------ -------------------------------------------------- -----
myRedirect.java
package com.util;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
public class myRedirect //Conversion class for url
{
public static String dealurl(HttpServletRequest request)
{
String url = "";
url = request.getRequestURL()+"?";
url +=param(request);
if(url.indexOf("&")>-1)
url=url.replaceAll("&","@#@");//In fact, it converts characters with & into @#@
return url;
}
public static String geturl(String url)//This is the restoration method
{
if(url.indexOf("@#@")>-1)
url=url.replaceAll("@#@","&");
return url;
}
public static String param(HttpServletRequest request)
{
String url = "";
Enumeration param = request.getParameterNames();//Get all parameter names
while(param.hasMoreElements())
{
String pname = param.nextElement().toString();
url += pname+"="+request.getParameter(pname)+"&";
}
if(url.endsWith("&"))
{
url = url.substring(0,url.lastIndexOf("&"));
}
return url;
}
}
In this way, the browser will automatically go to the address the user just requested when logging in.