发布于2021-05-29 19:12 阅读(704) 评论(0) 点赞(27) 收藏(5)
示例:使用Cookie保存用户名和密码,当用户再次登录时,在相应的文本栏显示上次登录时输入的信息。
(1) 编写用于接收用户输入的文件,在该例中,没有使用HTML文件而是用一个Servlet来完成此功能,这是因为需要通过Servlet去读取客户端的Cookie,而HTML文件无法完成此功能。Servlet 命名为LoginServlet,代码如下:
LoginServlet.java
public class LoginServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -3054516715115982849L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String cookieName="userName";
String cookiePwd="pwd";
//获得所有cookie
Cookie[] cookies=request.getCookies();
String userName="";
String pwd="";
String isChecked="";
//如果cookie数组不为null,说明曾经设置过
//也就是曾经登录过,那么取出上次登录的用户名,密码
if(cookies!=null&&cookies.length>0){
//如果曾经设置过cookie,checkbox状态应该是checked
isChecked="cheched";
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals(cookieName)){
userName=cookies[i].getValue();
}
if(cookies[i].getName().equals(cookiePwd)){
pwd=cookies[i].getValue();
}
}
}
response.setContentType("text/html;charset=GBK");
PrintWriter out=response.getWriter();
out.println("<html>\n");
out.println("<head><title>登录</title></head>\n");
out.println("<body>\n");
out.println("<center>\n");
out.println("<form action='CookieTest'"+"method='post'>\n");
out.print("姓名:<input type='text'"+"name='UserName'value='"+userName+"'><br/>\n");
out.print("密码:<input type='password'"+"name='Pwd'value='"+pwd+"'><br/>\n");
out.print("保存用户名和密码<input type='checkbox'"+"name='SaveCookie'value='Yes'"+isChecked+">\n");
out.print("<br/>\n");
out.print("<input type=\"submit\">\n");
out.print("</form>\n");
out.println("</center>\n");
out.println("</body>\n");
out.println("</html>\n");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
上述代码中,首先使用request.getCookies()获取客户端Cookie数组;再遍历该数组,找到对应的Cookie,取出用户名和密码:最后将信息显示在相应的表单控件中。
(2)新建CookieTest.java类,代码如下:
public class CookieTest extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -4464988342865948406L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie userCookie=new Cookie("userName",request.getParameter("UserName"));
Cookie pwdCookie=new Cookie("pwd",request.getParameter("Pwd"));
if(request.getParameter("SaveCookie")!=null&&request.getParameter("SaveCookie").equals("Yes")){
userCookie.setMaxAge(7*25*60*60);
pwdCookie.setMaxAge(7*25*60*60);
}else{
userCookie.setMaxAge(0);
pwdCookie.setMaxAge(0);
}
response.addCookie(userCookie);
response.addCookie(pwdCookie);
PrintWriter out=response.getWriter();
out.print("Welcome,"+request.getParameter("UserName"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
上述代码中,首先创建两个Cookie 对象,分别用来储存表单中传递过来的登录名和密码,然后根据客户端的“SaveCookie” 元素的值,决定是否向客户端发送Cookie, 或者删除以前存储的Cookie.
(3) 启动Tomcat, 在IE中访问http://localhost:8080/myapp/LoginServlet,运行结果如图示。
原文链接:https://blog.csdn.net/qq_50777680/article/details/117340261
作者:听说你没有见过我
链接:http://www.javaheidong.com/blog/article/207097/43bea9f66163d22d7ce4/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!