使用Cookie防刷新页面计数功能(纯源码+注解)

(1)功能描述(登录、复选框购书、退出)
(2)记录是第几位选书者(防止刷新的计数器,并且相同用户名下次登录进入选书页面,不进行累加,并显示上次登录的时间)

每行代码基本都有注释:

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登录页






(初始密码:123)

LoginServlet

package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {
	static int visitCount = 1;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8"); // 解决中文乱码 (如果删掉用不了中文)
		response.setHeader("content-type", "text/html;charset=UTF-8");
		// 获取用户名密码
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		
		System.out.println(name + password);
		if (name != "" && name != null && password.equals("123")) { // 校验用户名非空 以及 指定的密码 123
			// 创建cookie
			Cookie c1 = new Cookie("username", name);
			// 服务器将cookie发送给客户端,方便下一次请求时携带
			response.addCookie(c1);
			response.setHeader("username", name);

			System.out.println("登录成功");
			request.getSession().setAttribute("username", name); // 设置Session
			request.getRequestDispatcher("BookServlet").forward(request, response); // 跳转到BookServlet
		} else {
			System.out.println("登录失败");
			request.getRequestDispatcher("login.jsp").forward(request, response); // 跳转到login.jsp
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

BookServlet 

package com.servlet;

import java.io.IOException;
import java.util.Date;

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

public class BookServlet extends HttpServlet {
	static int visitCount = 0;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		response.setHeader("content-type", "text/html;charset=UTF-8");
		String username = (String) request.getSession().getAttribute("username"); // 设置Session  - 保存用户

		// 加入防刷机制
		boolean justVisit = false; // 是否在指定的时间内访问过
		Cookie[] cookies = request.getCookies(); // 获取Cookie数组
		if (null != cookies) {
			for (Cookie cookie : cookies) {
				if (username.equals(cookie.getName())) { // 如果已经登录了
					justVisit = true; break;
				}
			}
		}
		if (!justVisit) { // 如果最近没有访问过
			Cookie coo = new Cookie(username, "value");
			coo.setMaxAge(5 * 60); // 设置了最大保存时间
			response.addCookie(coo);
			visitCount = visitCount + 1; // 访问量+1
		}

		// 获取session创建时间
		Date createTime = new Date(request.getSession().getCreationTime());

		// 获取最后访问页面的时间
		Date lastAccessTime = new Date(request.getSession().getLastAccessedTime());

		System.out.println("访问量" + visitCount);
		request.getSession().setAttribute("visitCount", visitCount); // 保存人数
		request.getSession().setAttribute("lastAccessTime", lastAccessTime); // 最后登录时间 (页面没加)
		request.getSession().setAttribute("createTime", createTime); // 上次登录时间

		// 获取图书信息
		String category1 = request.getParameter("category1");
		String category2 = request.getParameter("category2");
		String category3 = request.getParameter("category3");
		String category4 = request.getParameter("category4");
		String name = (String) request.getSession().getAttribute("username"); // 获取
		System.out.println("用户名 :" + name + "   选择的图书:" + category1 + category2 + category3 + category4);

		request.getRequestDispatcher("book.jsp").forward(request, response); // 跳转页面
	}

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

}

Book.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



您是第${visitCount}位客人。

上次登录时间 ${createTime}
Number of visits ${visitCount}
<% String username = (String)request.getSession().getAttribute("username"); // 获取Session %> <%=username %> - 请选择你想看的书

JavaWeb核心技术

Java从入门到到精通

数据结构与算法分析

设计模式之美

退出

    //加入防刷机制
    boolean justVisit = false; //是否在指定的时间内访问过
    Cookie[] cookies = request.getCookies();
    if (null != cookies) {
        for (int i = cookies.length - 1; i >= 0; i--) {
            if (("Bwl_App_" + userName).equals(cookies[i].getName())) {
                justVisit = true; break;  }
        }
    }  if (!justVisit) { //如果最近没有访问过
        Cookie coo = new Cookie("Bwl_App_" + userName, "value");
        response.addCookie(coo);
    } -----liudaoru

 

 

 

你可能感兴趣的:(项目实战)