Summary: Little happy under, and learned something
-
JS Settings Cookie
-
Add Time function
-
function Setcookie (name, value) {
-
var days = 30;
-
var exp = new Date ();
-
Exp.settime (exp.gettime () + days * 24 * 60 * 60 * 1000);
-
document.cookie = name + "=" + Escape (value) + ";p ath=/;expires=" + exp.togmtstring (); Another key to learning
-
}
-
-
Get Time function
-
function GetCookie (name) {
-
var arr = Document.cookie.match (New RegExp ("(^|)" + name + "= ([^;] *)(;|$)"));
-
if (arr! = null) return unescape (arr[2]); return null;
-
-
}
-
-
Delete Time function
-
function Delcookie (name) {
-
var exp = new Date ();
-
Exp.settime (Exp.gettime ()-1);
-
var cval = GetCookie (name);
-
if (cval! = null) Document.cookie = name + "=" + Cval + ";p ath=/;expires=" + exp.togmtstring ();
-
}
Java Background settings cookie and value cookie
/**
* Business Card Login Request
* @param modelmap
* @param session
* @param response
* @param account
* @param password
* @param autologin
* @return
*/
@RequestMapping (value = "Crm_micro_business_card_login_card.do")
Public String Login (modelmap modelmap,httpsession session,httpservletresponse response, @RequestParam (value = "Account ", Required=false) String account,
@RequestParam (value = "Password", required=false) String password, @RequestParam (value = "Autologin", Required=false) String autologin) {
String forward = "redirect:/crm_micro_business_card_businesscardmanager_card.do";
try {
Crmmicrobusinesscardvo Crmmicrobusinesscardvo = Mpbasinfoservice.dologin (Account,password);
Cookie ckusername, Cksessionid;
if (autologin==null) {
Session.setattribute ("Crmmicrobusinesscardvo", CRMMICROBUSINESSCARDVO);
Forward = forward+ "? id=" +crmmicrobusinesscardvo.getid ();
}else if (Autologin.equals ("on")) {
ckusername = new Cookie ("Autologinuser", account);
Ckusername.setmaxage (60*60*24*30);
Response.addcookie (Ckusername); Save login user name to Cookie
String sessionId = Session.getid ();
Cksessionid = new Cookie ("SessionId", sessionId);
Cksessionid.setmaxage (60*60*24*30);
Response.addcookie (Cksessionid); Save Login SessionID to Cookie
Mpaccountsessionid Mpaccountsessionid = new Mpaccountsessionid ();
Mpaccountsessionid.setaccount (account);
Mpaccountsessionid.setsessionid (SESSIONID);
Mpbasinfoservice.savesessionid (Mpaccountsessionid); Save the login user name and SessionID to the database
Session.setattribute ("Crmmicrobusinesscardvo", CRMMICROBUSINESSCARDVO);
Forward = forward+ "? id=" +crmmicrobusinesscardvo.getid ();
}
} catch (Businesscardexception e) {
E.printstacktrace (); To change body of catch statement use File | Settings | File Templates.
Modelmap.put ("ErrorMsg", E.getmessage ());
Modelmap.put ("account", account);
Modelmap.put ("password", password);
Forward = "/tools/businesscardlogin";
}
return forward;
}
public void DoFilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain Filterchain) throws IOException, Servletexception {
To change body of implemented methods use File | Settings | File Templates.
HttpServletRequest request = (httpservletrequest) servletrequest;
HttpSession session = Request.getsession (true);
String account = "";
String sessionId = ""; This sessionid is the identification number that was saved to the client when the last user logged on, and is used for automatic login for subsequent user access. is not the session ID for this visit.
cookie[] cookies;
Cookiemanager cm = new Cookiemanager (); Cookiemanager is a custom class that is used to find and return a cookie value for a specified name from a cookie array.
Boolean isautologin;
If there is no user object in the session, create one.
Crmmicrobusinesscardvo Crmmicrobusinesscardvo = (CRMMICROBUSINESSCARDVO) session.getattribute (" Crmmicrobusinesscardvo ");
If the username of the user object is "", it means that the users are not logged in. The automatic logon process is performed.
Otherwise, it does not log on automatically.
if (CRMMICROBUSINESSCARDVO = = null) {
CRMMICROBUSINESSCARDVO = new Crmmicrobusinesscardvo ();
Check whether the user's browser sent the last logged-in user name and SessionID,
If yes, the user is automatically logged in.
cookies = request.getcookies ();
if (cookies!=null&&cookies.length>0) {
Account = Cm.getcookievalue (cookies, "Autologinuser");
SessionId = Cm.getcookievalue (cookies, "sessionId");
Account = "". Equals (account) Null:account;
SessionId = "". Equals (sessionId)? Null:sessionid;
map<string,object> map = new hashmap<string,object> ();
Map.put ("account", account);
Map.put ("SessionId", sessionId);
Boolean isexist = Basdao.isexist (Mpaccountsessionid.class,map);
if (isexist) {
Mpbasinfo Uservo = new Mpbasinfo ();
Uservo = Basdao.queryone (Mpbasinfo.class, "account", account);
if (uservo!=null) {
Crmmicrobusinesscardvo crmMicroBusinessCardVO1 = new Crmmicrobusinesscardvo ();
Utilbean.copybeanallsimpleproperties (Crmmicrobusinesscardvo1,uservo,false);
Session.setattribute ("Crmmicrobusinesscardvo", crmMicroBusinessCardVO1); Add the user bean to the session.
}
}
}
}
Filterchain.dofilter (ServletRequest, servletresponse);
}
public class Cookiemanager {
public static String getcookievalue(Cookie cookies[],string cookiename) {
String cookievalue = "";
for (int i=0;i<cookies.length;i++) {
Cookie cookie = cookies[i];
if (Cookie.getname (). Equals (CookieName)) {
Cookievalue = Cookie.getvalue (). toString ();
Break
}
}
return cookievalue;
}
This article is from the "Small blog" blog, be sure to keep this source http://9686567.blog.51cto.com/9676567/1606197
JS, Java background settings cookie