JS, Java background settings cookie

Source: Internet
Author: User

Summary: Little happy under, and learned something

  1. JS Settings Cookie


  2. Add Time function

  3. function Setcookie (name, value) {

  4. var days = 30;

  5. var exp = new Date ();

  6. Exp.settime (exp.gettime () + days * 24 * 60 * 60 * 1000);

  7. document.cookie = name + "=" + Escape (value) + ";p ath=/;expires=" + exp.togmtstring (); Another key to learning

  8. }


  9. Get Time function

  10. function GetCookie (name) {

  11. var arr = Document.cookie.match (New RegExp ("(^|)" + name + "= ([^;] *)(;|$)"));

  12. if (arr! = null) return unescape (arr[2]); return null;


  13. }


  14. Delete Time function

  15. function Delcookie (name) {

  16. var exp = new Date ();

  17. Exp.settime (Exp.gettime ()-1);

  18. var cval = GetCookie (name);

  19. if (cval! = null) Document.cookie = name + "=" + Cval + ";p ath=/;expires=" + exp.togmtstring ();

  20. }

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

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.