记账本开发记录——第十六天(2020.2.3)
写在前面:关于昨天的问题,在发完博客后捣鼓了俩小时搞好了。顺带着基本问题也解决了,是因为时区问题,c3p0默认时间为美国时间,而我们的电脑都是中国时间,时区不一致就没法用(咱也不知道为啥),至于数据库存储数据乱码问题,在连接数据库时写好连接的编码,并且在后台写好编码就可将问题解决。可以看到配置可真是个令人上头的玩意...
今日的学习:今天学习了会话技术Cookie&Session。通过这两个会话技术,可以解决很多业务上的基本问题,如验证码的校验,不同的用户显示不同的界面,如不同人的购物车不同,就可以通过设置session进行实现。
首先是会话技术的概念:从打开一个浏览器访问某个站点,到关闭这个浏览器的整个过程,称为一次会话。可以看到会话的概念是比较广泛的。在会话技术中有两种:cookie和session,cookie存在于本地,session存在于服务器端,由此便可看出cookie是可以被本浏览器清除的(清理垃圾时候的选项),而session存在于服务端,也就加大了服务端的压力。
下面是关于cookie的创建和发送的demo:
package cookie; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class SendCookieServlet */ @WebServlet("/Send") public class SendCookieServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SendCookieServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.创建cookie对象 Cookie cookie = new Cookie("name", "liuge"); //1.1 为cookie设置持久化时间-----存放在磁盘上的时间 cookie.setMaxAge(60*10);//10分钟----时间设置为0代表删除cookie //1.2 为cookie设置携带的路径 //cookie.setPath("/Day16/Send");//访问Send资源时才携带cookie //cookie.setPath("/Day16");//访问Day16下的所有资源都携带cookie //cookie.setPath("/");//访问目前tomcat下的所有的资源都携带cookie //2.将cookie中存储的信息发送到客户端——头 response.addCookie(cookie); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
得到cookie的demo:
package cookie; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class GetCookieServlet */ @WebServlet("/GetCookieServlet") public class GetCookieServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public GetCookieServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得客户端携带的cookie数据 Cookie[] cookies = request.getCookies(); //通过cookie的名称获得想要的cookie for(Cookie cookie:cookies) { //获得cookie的名称 String cookiename = cookie.getName(); if(cookie!=null) { if(cookiename.equals("name")) { //获得该cookie的值 String cookieValue = cookie.getValue(); System.out.println(cookieValue); } } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
根据cookie的特性,做了一个显示上回访问时间的demo:
package lastaccesstime; import java.io.IOException; import java.util.Date; import java.text.SimpleDateFormat; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.mysql.fabric.xmlrpc.base.Data; /** * Servlet implementation class LastAccessTimeServlet */ @WebServlet("/LastAccessTime") public class LastAccessTimeServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LastAccessTimeServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得当前时间 Date date =new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss"); String currentTime = format.format(date); //1.创建cookie 记录当前的最新的访问时间 Cookie cookie = new Cookie("lastAccessTime",currentTime); cookie.setMaxAge(60*10*500); response.addCookie(cookie); //2.获得客户端携带的cookie ----lastAccessTime String lastAccessTime = null; Cookie[] cookies = request.getCookies(); if(cookies!=null) { for(Cookie coo:cookies) { if("lastAccessTime".equals(coo.getName())) { lastAccessTime = coo.getValue(); } } } response.setContentType("text/html;charset=UTF-8"); if(lastAccessTime==null) { response.getWriter().write("您是第一次访问"); } else { response.getWriter().write("您上次访问时间是:"+lastAccessTime); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
需要注意的是,新版本的tomcat无法存储空格,cookie也无法存储中文。
之后是关于session的创建和发送demo:
package session; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class SessionServlet1 */ @WebServlet("/SessionServlet1") public class SessionServlet1 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SessionServlet1() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //创建该客户端(会话)的私有session区域 /*request.getSession()方法内部会判断该客户端是否在服务器端已经存在session * 如果该客户端在此服务器不存在session 那么就会创建一个新的session对象 * 如果该客户端在服务求已存在session 就会获得已经存在的该session返回 */ HttpSession session = request.getSession(); session.setAttribute("name", "tom"); String id = session.getId();//该session对象的id //手动创建一个存储JSESIONID的Cookie 为该cookie设置持久时间 Cookie cookie = new Cookie("JSESSIONID", id); cookie.setPath("/Day16/"); cookie.setMaxAge(60*10); response.addCookie(cookie); response.getWriter().write("JSESSIONID:"+id); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
以及显示session的demo:
package session; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class SessionServlet2 */ @WebServlet("/Session2") public class SessionServlet2 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SessionServlet2() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //从session中获得存储的数据 HttpSession session = request.getSession(); String attribute = (String) session.getAttribute("name"); response.getWriter().write(attribute); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
最后,根据session实现了一个验证码校验功能:
package login; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Login */ @WebServlet("/Login") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //验证码的校验 //获得页面输入的验证 request.setCharacterEncoding("UTF-8"); String checkCode_client = request.getParameter("checkCode"); //获得生成图片的文字的验证码 String checkCode_session = (String) request.getSession().getAttribute("checkcode_session"); //将二者进行比对 if(!checkCode_session.equals(checkCode_client)) { request.setAttribute("loginInfo", "您的验证码不正确"); request.getRequestDispatcher("/login.jsp").forward(request, response); return; } //获得页面的用户名和密码进行数据库的校验 //... } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
今日总结:可以看到,通过cookie和session的使用,可以实现一些很简单的业务流程。日后也可在自己的网站中实现。