application实现用户之间的数据共享
session时长由浏览器决定,application时长由容器决定,容器默认1800;
application是应用级范围对象,能实现用户的数据共享,通常下,用于服务器,logo等一成不变的情境下
application对象类似于系统的“全局变量”,用于同一个应用内的所有用户之间的数据共享。
<%
Integer count=(Integer)application.getAttribute("count");
if(count!=null){
count=count+1;
}else{
count=1;
}
application.setAttribute("count",count);
%>
<%
Integer i=(Integer)application.getAttribute("count");
out.print("统计访问量:目前有"+i+"个人访问过本网站");
%>
它是最高作用域
四大作用域:- pageContext(作用域指本JSP页面范围)
- request(request作用域内的对象则是与客户端的请求绑定在一起的)
- session(作用域:一次会话)
- application(面对整个Web应用程序,没有具体指向)
..config:做页面中对应Servlet的配置
..exception:异常,页面中的异常信息
page Cookie是存储在客户机的文本文件,它们保存了大量信息。 作用:当我们打开一个网站时,如果这个网站我们曾经登录过,那么当我们再次打开网站时,发现就不需要再次登录了,而是直接进入了首页 创建与获得 //设置存储路径范围 cookie.setPath(""); //设置最大毫秒数 cookie.setMaxage(3600); //写入 response.addCookie(cookie); public classCookieDemo002{ Cookie[] cookies = request.getCookies; for(Cookie cookie:cookies){ if(判断是不是所需对象){ out.print(xxxx)} } } 他是前端的脚本文件,是一个文本文件,安全性低,容易泄露信息,他只能读取本地域名下的资源,是web服务器保存在客户端的一系列文本信息。 JavaBean(封装数据,封装业务) JavaBean 是一种java语言写成的可重用组件。为写成JavaBean,类必须是具体的和公共的,并且具有无参数构造器。JavaBean 通过提供符合一致性设计模式的公共方法将内部域暴露成员属性,set和get方法获取。众所周知,属性名称符合这种模式,其他Java 类可以通过自省机制(反射机制)发现和操作这些JavaBean 的属性。 通过Properties类来实现读取.properties文件 resource->db.properties utils->JDBCUtils <%=request.getContextPath()%>放css前,图片前,保证不乱码
public class CookieDemo001 {
Cookie cookie =new Cookie();
}
classname=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/easybuy
username=root
password=root
package com.csi.eshop.utils;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private Properties properties;
protected Connection getConnection() throws SQLException {
//构建对象
properties = new Properties();
try {
properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties"));
} catch (IOException e) {
e.printStackTrace();
}
try {
Class.forName(getValue("classname"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = DriverManager.getConnection(getValue("url"), getValue("username"), getValue("password"));
return connection;
}
public String getValue(String key){
return properties.getProperty(key);
}
public static void main(String[] args) throws SQLException {
JDBCUtils jdbcUtils = new JDBCUtils();
System.out.println(jdbcUtils.getConnection());
}
protected void release(Connection connection) {
if(connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
protected void release(Statement statement) {
if(statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
protected void release(ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
protected void release(PreparedStatement ps,Connection connection) {
release(ps);
release(connection);
}
protected void release(ResultSet rs, PreparedStatement ps, Connection connection) {
release(rs);
release(ps,connection);
}
}



