关闭

Cookie——项目实战:运用Cookie技术,显示用户上次登录的时间

标签: cookie技术
427人阅读 评论(0) 收藏 举报
分类:
<span style="font-size:18px;">package MyFirstservlet;

import java.io.*;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;

 public class FirstServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html charset=utf-8");
		PrintWriter out = response.getWriter();
		       
		out.println("<h1 style='color:red'>开源</h1><br/>");
        out.println("<font color='green'>单身狗,欢迎你!</font><br/>");
        // 提交的表单
        out.println("<form action='/Myservlet/yy' method='post'>");
        out.println("姓 名:<input type='text' name='username'/><br/>");
        out.println("密 码:<input type='password' name='password'/><br/>");
        out.println("<input type='submit' value='登陆'>");
        out.println("</form>");
		 
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package MyFirstservlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class success extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        Cookie cookies[] = request.getCookies(); //服务器端接收客户端传来的Cookie
        
        if(request.getCookies()==null){
            
            //设定时间的格式
            SimpleDateFormat slf1 = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
            String time = slf1.format(new Date());
            
            //在服务器端创建一个Cookie来存储一组值; 
            Cookie cookie = new Cookie("lasttime",time); 
            cookie.setMaxAge(7*24*3600); //设定保存的最长时间
            response.addCookie(cookie);  //服务器返回给客户端
            out.println("<h1>欢迎光临本店</h1>");
        }
        
        String value=null;
        if(request.getCookies()!=null){
            
            for(int i=0;i<cookies.length;i++){
                String name = cookies[i].getName();
                if("lasttime".equals(name)){
                     
                    value = cookies[i].getValue();
                    out.println("<h1>小咸菜!你好啊!</h1>");
                    out.println("您上次登录的时间是:"+ value);
                    
                    SimpleDateFormat slf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
                    String time =  slf.format(new Date());
                 
                    Cookie cookie1 = new Cookie("lasttime",time); 
                    cookie1.setMaxAge(7*24*3600);
                    response.addCookie(cookie1);
                    
                }
        
        }
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            this.doGet(request, response);
         
    }

}
+++++++++++++++++++++++++++++++++++++++++++++++++
package MyFirstservlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class yy extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
               request.setCharacterEncoding("utf-8");
               response.setContentType("text/html charset=utf-8");
               response.setCharacterEncoding("utf-8");
               PrintWriter out = response.getWriter();
               out.println("<h1>我爱你,芳芳!<h1>");
               String username = request.getParameter("username");
               String password = request.getParameter("password");
               if(("hlq".equals(username))&&("123".equals(password))){
                   
                   response.sendRedirect("/Myservlet/success");
               }else {
                   response.sendRedirect("/Myservlet/yy");
               }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
          this.doGet(request, response);
    }

}






 </span>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3277次
    • 积分:103
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类