cookie 简单案例

Login.jsp



	
name:


index.jsp



<%
	String name = request.getParameter("name");
	if(name != null && !name.trim().equals("")){
		Cookie cookie = new Cookie("name",name);
		cookie.setMaxAge(20);
		response.addCookie(cookie);
	}else{
		Cookie[] cookies = request.getCookies();
		if(cookies != null && cookies.length > 0 ){
			for(Cookie cookie :cookies){
				String cookieName = cookie.getName();
				if("name".equals(cookieName)){
					String vla = cookie.getValue();
					name = vla;
				}
			}
		}
	}
	
	if(name != null&& !name.trim().equals("")){
		out.print("Hello:" + name);
	}else{
		response.sendRedirect("Login.jsp");
	}
	
%>
	


你可能感兴趣的:(cookie)