因为有两个值,用户名和密码,但要保存到一个cookie中,所以做了拼接的方法,拼接的方法做好了请问拆解的方法怎么写.代码如下,里面用到的loginName和passwd在上面已有定义:writecookie方法已经写好,就是想知道readcookie怎么写
public void writeCookie(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Cookie c;
System.out.println(loginName);
String s = loginName + "|" + passwd;
c = new Cookie("TEST", s);
resp.addCookie;
c.setMaxAge(20);
} public void readCookie(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Cookie cookie[] = request.getCookies();
javax.servlet.http.HttpSession session;
session = request.getSession();
java.io.PrintWriter out = response.getWriter();
if (cookie == null || cookie.length == 0) {
out.print("没有cookie");
} else {
for (int i = 0; i < cookie.length; i++) {
Cookie c = cookie[i];
String susername = c.getName();
String spassword = c.getName();
System.out.println(c.getPath());
if(susername.equals("loginName"))
{
String username = c.getValue();
session.setAttribute(susername, username);
}
if(spassword.equals("passwd"))
{
String password = c.getValue(); session.setAttribute(spassword, password);
}
}
}
}
public void writeCookie(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Cookie c;
System.out.println(loginName);
String s = loginName + "|" + passwd;
c = new Cookie("TEST", s);
resp.addCookie;
c.setMaxAge(20);
} public void readCookie(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Cookie cookie[] = request.getCookies();
javax.servlet.http.HttpSession session;
session = request.getSession();
java.io.PrintWriter out = response.getWriter();
if (cookie == null || cookie.length == 0) {
out.print("没有cookie");
} else {
for (int i = 0; i < cookie.length; i++) {
Cookie c = cookie[i];
String susername = c.getName();
String spassword = c.getName();
System.out.println(c.getPath());
if(susername.equals("loginName"))
{
String username = c.getValue();
session.setAttribute(susername, username);
}
if(spassword.equals("passwd"))
{
String password = c.getValue(); session.setAttribute(spassword, password);
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货