JSP&Servlet学习笔记1

JSP设置Cookie,保存用户名和密码

1.客户端网页

<%

Cookie[] cookies=request.getCookies();

String userName=null;

String password=null;

for(int i=0;cookies!=null&&i

if("userName".equals(cookies[i].getName())){

userName=cookies[i].getValue();

}

if("password".equals(cookies[i].getName())){

password=cookies[i].getValue();

}

}

if(userName==null){

userName="";

}

if(password==null){

password="";

}

%>

2.服务端网页

<%@ page input ="java.util.*" %>

<%

String userName=request.getParameter("userName");

String password=request.getParameter("password");

Cookie cookie1=new Cookie("userName",userName);

cookie1.setMaxAge(60*60*24*7);

Cookie cookie2=new Cookie("password",password);

cookie2.setMaxAge(60*60*24*7);

if("remenber-yes".equals(request.getParameter("remenber")){

response.addtCookie(cookie1);

response.addCookie(cookie2);

}

response.senRedirect("client.jsp");

你可能感兴趣的:(JSP&Servlet学习笔记1)