1. Cookies
1. Set cookies, the content is time
Cookie cookie = new Cookie("LastAccessTime",System.currentTimeMillis()+"");cookie.setMaxAge(1*30*24*3600);//Set validity period of 1 month cookie.setPath("/Project name");//Cookie is provided when accessing the entire project2. Obtain cookie information
Cookie cookies[] = request.getCookies(); for(int i = 0;cookie!=null&&i<cookies.length;i++){ if(cookies[i].getName().equals("LastAccessTime")){ long cookieValues = Long.parseLong(cookies[i].getVlues());//Convert String to a decimal Long Date date = new Date(cookieValues); response.getWrite().print(date); }}2. session(getSession()->session not used for 30 minutes)
1. Set session
HttpSession session = request.getSession();session.setAttribute("name","hahahahahaha");2. Get session
HttpSession session = request.getSession();//HttpSession session = request.getSession(false);//Only get without creating String str = (String)session.getAttribute("name");3. Session configuration, configuration time
<seeion-config> <session-timeout>20</session-timeout> </session-config>
4. Session destroy
HttpSession session = request.getSession();session.invalidate();//session.removeAttribute("XXX");//removeAttribute("XXX");//remove a session5. Use address rewriting to get session, which will only be rewritten if cookie is disabled.
request.getSession();String url1 = response.encodingURL("Address 1 that needs to be rewrite");String url2 = response.encodingURL("Address 2 that needs to be rewrite");PrintWriter out = response.getWriter;out.print("<a href = '"+url1+"'>XXXX</a>");out.print("<a href = '"+url2+"'>YYYY</a>");3. Client form submission issues
1. Prevent submission of empty passwords
<form action="/project/xxx" method ="post" onsubmit="return dosubmit(this)"> Username:<input type="text" name="username"><br/> Password:<input type="password" name = "password"><br/> <input type="submit" value="submit"></form><script> function dosubmit(obj){ if(obj.category.value==''){ alter("Please enter"); return false; } }</script>2. Prevent repeated submissions
<form action="/project/xxx" method ="post" onsubmit="return dosubmit()"> Username:<input type="text" name="username"><br/> Password:<input type="password" name = "password"><br/> <input type="submit" value="submit"></form><script> function dosubmit(){ var iscommitted = false; if(!iscommitted){ iscommitted = true; return true; }else{ return false; } }</script>Summarize
The above is all the content of this article about the basic analysis of Servlet session technology, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!