웹 사 이 트 는 지난번 방문 시간 servlet 실현 을 기억 합 니 다.

4071 단어 servletcookie자바
수요
4
  • servlet 를 방문 합 니 다.첫 번 째 방문 이 라면 안녕하세요?첫 방문 을 환영 합 니 다
  • 4.567917.그렇지 않 으 면 돌아 온 것 을 환영 합 니 다.마지막 방문 시간 을 표시 합 니 다.4.567918.
    분석:쿠키 로 완성 가능
    서버 에 servlet 에서 last time 이라는 쿠키 가 있 는 지 판단 합 니 다.
    처음 방문 한 것 이 아 닙 니 다.
    처음 방문
    응답 데이터
    쿠키
    public class Cookietest extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //              
            response.setContentType("text/html;charset=utf-8");
            boolean flag = false;
            //     cookie
            Cookie[] cc = request.getCookies();
            if(cc!=null && cc.length>0){
                for(Cookie c : cc){
                    if("lastTime".equals(c.getName())){
                        //  cookie value
    //                      cookie  
                         String v = c.getValue();
                        v= URLDecoder.decode(v,"utf-8");//url  
                        response.getWriter().write("      "+v);
                        //          
                        Date date = new Date();
                        SimpleDateFormat sd = new SimpleDateFormat("yyyy MM dd  HH:mm:ss");
                        String st_date = sd.format(date);
                        st_date = URLEncoder.encode(st_date,"utf-8");//url  
                        c.setValue(st_date);
                        response.addCookie(c);
                        flag=true;
                        //  cookie     
                        c.setMaxAge(60*60*24*30);
    
    
                       
                        break;
                    }
    
                }
            }
            if(cc == null || cc.length==0 || flag == false) {
                //          
                Date date = new Date();
                SimpleDateFormat sd = new SimpleDateFormat("yyyy MM dd  HH:mm:ss");
                String st_date = sd.format(date);
                st_date = URLEncoder.encode(st_date,"utf-8");//url  
                Cookie lastTime = new Cookie("lastTime", st_date);
                response.addCookie(lastTime);
                //  cookie     
                lastTime.setMaxAge(60*60*24*30);
                st_date =  URLDecoder.decode(st_date,"utf-8");
                response.getWriter().write("

    " + st_date+"

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


    jsp 스타일
    
    
    
    
    
    
    
        Title
    
    
    0){
            for(Cookie c : cc){
                if("lastTime".equals(c.getName())){
                    //  cookie value
                    String v = c.getValue();
                    v= URLDecoder.decode(v,"utf-8");//url  
                    response.getWriter().write("      "+v);
    //                      cookie  
                    //          
                    Date date = new Date();
                    SimpleDateFormat sd = new SimpleDateFormat("yyyy MM dd  HH:mm:ss");
                    String st_date = sd.format(date);
                    st_date = URLEncoder.encode(st_date,"utf-8");//url  
                    c.setValue(st_date);
                    response.addCookie(c);
                    flag=true;
                    //  cookie     
                    c.setMaxAge(60*60*24*30);
    
    
    
                    break;
                }
    
            }
        }
        if(cc == null || cc.length==0 || flag == false) {
            //          
            Date date = new Date();
            SimpleDateFormat sd = new SimpleDateFormat("yyyy MM dd  HH:mm:ss");
            String st_date = sd.format(date);
            st_date = URLEncoder.encode(st_date,"utf-8");//url  
            Cookie lastTime = new Cookie("lastTime", st_date);
            response.addCookie(lastTime);
            //  cookie     
            lastTime.setMaxAge(60*60*24*30);
            st_date =  URLDecoder.decode(st_date,"utf-8");
            response.getWriter().write("

    " + st_date+"

    "); } %>


    좋은 웹페이지 즐겨찾기