html+jsp个人网站模板,wordpress 备份 迁移,嘉兴百度网站推广,做地图特效的网站会话
会话#xff1a;指的是一个客户端#xff08;浏览器#xff09;与Web服务器之间连续发生的一系列请求和响应的过程。
客户端和服务器的请求和响应的过程#xff08;对话双方只要有一方发生变化#xff0c;都属于不同的会话#xff09; 超时间隔【距离上一次请求的…会话
会话指的是一个客户端浏览器与Web服务器之间连续发生的一系列请求和响应的过程。
客户端和服务器的请求和响应的过程对话双方只要有一方发生变化都属于不同的会话 超时间隔【距离上一次请求的间隔】超时后就属于不同的会话 HTTP是无状态的不保存用户信息 Cookie客户端 Session服务器
Cookie Cookie是一种会话技术它用于将会话过程中的数据保存到用户的浏览器中【保存在客户端的磁盘或缓存内存中】从而使浏览器和服务器可以更好地进行数据交互。 用户第一次访问时没有Cookie
Cookie API Cookie的相关方法
Cookie–setMaxAge()和getMaxAge()
负数浏览器一关缓存就会清空【将Cookie保存在浏览器的缓存中】 默认为-1 Cookie的案例
/*** Illustration** author dengqing* time 2021/10/13* function cookie上次访问时间*/WebServlet(name Cookie1, value /cookie1)
public class Cookie1 extends HttpServlet {Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType(text/html; charsetUTF-8);//字符输出流PrintWriter out response.getWriter();Date date new Date();//格式化输出SimpleDateFormat sdf new SimpleDateFormat(yyyy年MM月dd日HH:mm:ss);//获取当前时间String NowTime sdf.format(date);//创建Cookie对象并保存当前时间到Cookie对象之中Cookie cookie new Cookie(LastTime, NowTime);//将Cookie信息回写至客户端浏览器response.addCookie(cookie);//设置cookie保存在磁盘中为90秒90秒后失效cookie.setMaxAge(90);//把浏览器中所有Cookie返回//Cookie[] cookies:Cookie对象数组Cookie[] cookies request.getCookies();String LastAccessTime null;//如果cookies不为空再循环防止空指针异常if (cookies ! null) {//增强型for循环for (Cookie c : cookies) {//匹配是否有LastTime Cookie//LastTime字符串写在前面防止空指针异常if (LastTime.equals(c.getName())) {//获取Cookie的值如果为空则为浏览器第一次访问LastAccessTime c.getValue();}}}//Cookie的值如果为空则为浏览器第一次访问if (LastAccessTime.isEmpty()) {out.write(你是首次访问本站);} else {//每次刷新就会重新计算90秒cookie.setMaxAge(90);//90秒后失效,就又会显示你是首次访问本站out.write(你上次访问本站的时间 LastAccessTime);}}Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
}
Session会话
Cookie是键值对不能存储大量数据【每次放在请求头中】并且不安全效率低 所以使用Session存储大量数据Session是一种将会话数据保存到服务器端的技术 说会话指Session
Session的创建由Servlet容器在发起会话时自动创建 当浏览器访问Web服务器时Servlet容器就会创建一个Session对象和ID属性【32位16进制16的32次方2^128次方ID不会重复类似IPv6,64位】当客户端后续访问服务器时只要将标识号传递给服务器服务器就能判断出该请求是哪个客户端发送的从而选择与之对应的Session对象为其服务。
由于客户端需要接收、记录和回送Session对象的ID因此通常情况下Session是借助Cookie技术来传递ID属性的。
Session原理 Session是通过Cookie技术实现的依赖于名为JSESSIONID的Cookie它将信息保存在服务器端。Session中能够存储复杂的Java对象因此使用更加方便。如果客户端不支持Cookie或者禁用了Cookie仍然可以通过使用URL重写来使用Session。 Session-获取Session对象
不同的请求对象获取的Session对象不一定不同因为可能是处于同一次会话 同一个浏览器的不同窗口是同一个Session;不同的浏览器是不同Session Session相关方法
大型项目一般使用时间戳国内外一致【getLastAccessedTime()】 Sesssion超时设置为分钟在超时时间内如果没有任何请求则超时
invalidate():类似删除Session Tomcat的Session超时设置 Session案例
浏览器不同窗口属于同一次会话 浏览器关闭则结束会话了 loginServlet.html
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
body
!--等效--
!--actionhttp://localhost:8082/WebServletProject_war_exploded/loginServlet--
form actionloginServlet!--div标签把组件分割开--!--placeholder 是HTML5 中新增的一个属性。placeholder可以用来描述输入字段预期值的简短的提示信息。提示信息会在用户输入值之前显示一旦用户输入信息该提示就会自动消失。--!--placeholder :提示用户输入信息--divinput typetext nameuname placeholder用户名/divinput typepassword nameupwd placeholder密码/brdivinput typesubmit value登录/div/form
/body
/htmlLoginServlet.java
/*** Illustration** author dengqing* time 2021/10/13* function Session:实现登录成功后存入Session;获取Sesssion数据*///http://localhost:8082/WebServletProject_war_exploded/loginServlet.html
WebServlet(name LoginServlet, value /loginServlet)
public class LoginServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType(text/html;charsetUTF-8);PrintWriter out response.getWriter();System.out.println(进入loginServlet登录页面...);//获取login.html页面用户输入的值String name request.getParameter(uname);String pwd request.getParameter(upwd);if (admin.equals(name) 123.equals(pwd)) {//获取对话SessionHttpSession session request.getSession();//将当前用户的名称存入Sessionsession.setAttribute(user,name);//scriptalert(登录成功)/script:JavaScript弹出警告框out.write(scriptalert(登录成功)/script);response.sendRedirect(mainServlet);} else {out.write(用户名或密码输入错误);response.sendRedirect(loginServlet.html);}}Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
}
MainServlet.java
WebServlet(name MainServlet, value /mainServlet)
public class MainServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType(text/html;charsetUTF-8);PrintWriter out response.getWriter();//获取当前对话的Session对象HttpSession session request.getSession();//获取Session的用户名称Object user session.getAttribute(user);//不为空之前已经登录过直接访问if (user!null){out.write(进入MainServelt主页面);out.write(欢迎回来(String)user);}else {out.write(你还没有登录请先登录后再访问此页面\n);out.write(a hrefloginServlet.html点击此处进行登录/a);}}Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
}
Java Bean
JavaBean符合一定规范写的Java类是一种规范。它的方法命名构造以及行为必须符合特定的要求 1.所有属性为private 2.这个类必须具有一个公共的public无参构造函数 3.private属性必须提供public的getter和setter来给外部访问并且方法的命名也必须遵循一定的命名规范 4.这个类是可序列化的要实现serializable接口
JavaBean类必须是具体的和公共的并且具有无参数的构造器。JavaBean 通过提供符合一致性设计模式的公共方法将内部域暴露成员属性。众所周知属性名称符合这种模式其他Java 类可以通过自身机制发现和操作这些JavaBean 的属性。