A method for implementing memory redirection for JSP website login

Source: Internet
Author: User

You can use getHeader ("Referer") when logging on to the website. The method is simple, but the post parameter cannot be processed. It is originally used in struts and webwork, to make it easy to understand, rewrite it to the 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 interfering with the request url because it has a & character

Response. sendRedirect ("weblogin. jsp? Url = "+ url); // redirect to the logon page

}

%>

------------------------------------------------

Weblogin. jsp

<%

String url = request. getParameter ("url ");

String userid = request. getParameter ("userid ");

String password = request. getParameter ("password ");

If (userid! = Null) // if it is a logon submission, execute the following code

{

If (logon successful)

{

Session. setAttribute ("userid", userid );

Out. println ("<script> alert (LOGIN successful, thank you for coming); self. location. href = "+ myRedirect. geturl (url) + "; </script>"); // redirects to the url of the first request ,. geturl is used to convert the converted address back into a real url.

} Else {

Out. println ("<script> alert (incorrect user name or password); history. back (); </script> ");

}

} Else {// if not submitted for logon, the current logon page is displayed.

%>

<Table width = "311" height = "162" border = "0" align = "center" cellpadding = "0" cellspacing = "0">

<Tr>

<Td width = "311" height = "162" align = "center"> you have not logged on. Please log on 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 & nbsp; No.: </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 & nbsp; Code: </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 = "Logon">

& Nbsp; <a href = "reg. jsp" target = "_ blank"> not registered </a> & nbsp; </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 %>"> here we need a hidden to save it. Otherwise, the submission will be gone.

</Form>

</Table>

</Td>

</Tr>

</Table>

<%

}

%>

---------------------------------------------------------------

MyRedirect. java

Package com. util;

Import javax. servlet. http. HttpServletRequest;

Import java. util. Enumeration;

Public class myRedirect // url conversion class

{

Public static String dealurl (HttpServletRequest request)

{

String url = "";

Url = request. getRequestURL () + "? ";

Url + = param (request );

If (url. indexOf ("&")>-1)

Url = url. replaceAll ("&", "# @"); // actually, it is to convert the & characters @#@

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 (); // obtain 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 address requested by the user will be automatically forwarded to the browser.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.