日期:2014-05-20 浏览次数:20797 次
<% //从提交的HTML表单中获取,用户名 String userName=request.getParameter("username"); //以"username", userName 值/对 创建一个Cookie Cookie theUsername=new Cookie("username",userName); response.addCookie(theUsername); %> .............. <% Cookie myCookie[]=request.getCookies();//创建一个Cookie对象数组 for(int n=0;n=cookie.length-1;i++);//设立一个循环,来访问Cookie对象数组的每一个元素 Cookie newCookie= myCookie[n]; if(newCookie.getName().equals("username")); //判断元素的值是否为username中的值 {%> 你好,<%=newCookie.getValue()%>!//如果找到后,向他问好 <%} %>
------解决方案--------------------
<%Cookie[] cookies = request.getCookies(); String user_name = ""; String user_pass = ""; if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals("userName")) { user_name = cookies[i].getValue(); } else if (cookies[i].getName().equals("userPass")) { user_pass = cookies[i].getValue(); } } } %> <center> <html:form action="login.do" method="post" onsubmit="return checkLogin();"> <table class="login"> <tr> <td> <img src="imgs/login_center.jpg"> </td> </tr> <tr> <td> 用户名 <input title="java" type="text" name="name" id="login_account" style="width:150px" value="<%=user_name %>"> </td> </tr> <tr> <td> <font class="warning"> <%=info%> </font> 密 码 <input title="java" type="password" name="pass" id="login_password" style="width:150px" value="<%=user_pass %>"> </td> </tr> <tr> <td> <input class="btn" type="submit" value="登录"> </td> </tr> </table> </html:form>