JSP Remember password

Source: Internet
Author: User

I found code copy with similar functions on the Internet and modified it. So I posted it and took a note...

 

Index. jsp

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8"
Contenttype = "text/html; charset = GBK" %>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "C" %>

<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + "://"
+ Request. getservername () + ":" + request. getserverport ()
+ Path + "/";

String name = NULL;
String pass = NULL;

Cookie [] cookies = request. getcookies ();
For (INT I = 0; cookies! = NULL & I <cookies. length; I ++ ){
If (Cookies [I]. getname (). Equals ("user ")){
Name = Cookies [I]. getvalue (). Split ("-") [0];
Pass = Cookies [I]. getvalue (). Split ("-") [1];
}
}
If (name = NULL ){
Name = "";
}
If (pass = NULL ){
Pass = "";
}
%>
<HTML>
<Head>

<SCRIPT type = "text/JavaScript">

Function check (){
VaR TMP = Document. getelementbyid ("CK1"). checked;
If (TMP! = "False "){
Document. getelementbyid ("deleted"). Disabled = false;
} Else {
Document. getelementbyid ("deleted"). Disabled = true;
}
}
Window. onload = function (){
If (<% = Name %>! = NULL ){
Document. getelementbyid ("ck"). Checked = true;
}
}
</SCRIPT>

<Base href = "<% = basepath %>">
<Title> logon page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK">
</Head>

<Body>
<P> & nbsp; </P>
<Form action = "checklogin. jsp">
<Table width = "388" border = "0" align = "center" cellspacing = "1" bordercolor = "# 3399cc">
<Tr align = "center">
<TD colspan = "2" Height = "59"> <font size = "6"> <B> <font color = "#330099" size = "5"> log on as an administrator </font> </B> </font>
</TD>
</Tr>
<Tr>
<TD width = "96" align = "right"> pass: </TD>
& Lt; TD width = "154" & gt;
<Input type = "text" name = "name" size = "15" value = "<% = Name %>"/>
</TD>
</Tr>
<Tr>
<TD width = "96" align = "right"> password: </TD>
& Lt; TD width = "154" & gt;
<Input type = "password" name = "pass" size = "15" value = "<% = pass %>"/>
</TD>
</Tr>
<Tr>
<TD width = "96" align = "right"> </TD>
& Lt; TD width = "154" & gt;
<Input type = "checkbox" name = "ck" value = "1"/> & nbsp; remember the password
</TD>
</Tr>
<Tr>
<TD width = "96" align = "right"> </TD>
& Lt; TD width = "154" & gt;
<Input type = "checkbox" name = "CK1" value = "1" id = "CK1"/> & nbsp; <a href = "#"> I agree to the above terms </a>
</TD>
</Tr>
<Tr align = "center">
<TD colspan = "2">
<Input type = "Submit" name = "login" value = "login"/>
<Input type = "reset" name = "reset" value = "">
</TD>
</Tr>
</Table>
</Form>

</Body>
</Html>

 

Checklogin. jsp

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "ISO-8859-1" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + "://"
+ Request. getservername () + ":" + request. getserverport ()
+ Path + "/";

String name = request. getparameter ("name ");
String pass = request. getparameter ("pass ");
String ck = request. getparameter ("ck ");
If (Ck = NULL ){
Ck = "0 ";
}

If (CK. Equals ("1 ")){
Try {
Cookie user = new cookie ("user", name + "-" + pass );
User. setmaxage (60 );
Response. addcookie (User );
} Catch (exception e ){
E. printstacktrace ();
}

}
%>

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">

<Title> my JSP 'checklogin. jsp 'starting page </title>

<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->

</Head>

<Body>
This is my JSP page.
<Br>
</Body>
</Html>

 

PS: directly publish it to Tomcat and run it...

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.