线程安全类
HashMap (非线程安全)
ConcurrentHashMap (线程安全)
ArrayList (非线程安全)
CopyOnWriteArrayList (线程安全)
StringBuffer 字符串变量(线程安全)
StringBuilder 字符串变量(非线程安全)
线程安全的有锁,需要等待,性能会弱一些
web开发技术之jstl
依赖
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
jsp页面依赖
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
多重选择支持表达式语言
<c:choose>
<c:when test="${age<12}">
小学阶段
</c:when>
<c:when test="${age>=12&&age<16}">
中学阶段
</c:when>
<c:when test="${age>=16&&age<30}">
大学阶段
</c:when>
<c:otherwise>
你猜
</c:otherwise>
</c:choose>
jstl实现循环
<c:forEach var="i" begin="1" end="10" step="1">
第<c:out value="${i}" />行
<br>
</c:forEach>
监听器Listener
listener作用
Listener是监听器,它可以监听客户端的请 求、服务端的操作等。通过监听器,可以 自动激发一些操作
• 1 Servlet 和Lisneter区别
• 2 监听系统启动,做一些初始化动作
• 3 比如监听在线的用户的数量
listener模板创建
new–>other–>listener
分类
session和request区别
Request 是当前请求连接传输的数据
Session 购物车 是服务端为用户分配的缓 存(1024k)
• Cookie? 浏览器缓存
• Cookie 和Session关系 sessionId
• Session 超时问题
https://www.cnblogs.com/sharpest/p/6185234.html
• 浏览器清空缓存
监听在线的用户的数量例子
通过listener模板创建
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* Application Lifecycle Listener implementation class Listener
*
*/
@WebListener
public class SessionCountListener implements HttpSessionListener {
private static int alluser;
private static final String lock="lock";
/**
* Default constructor.
*/
public SessionCountListener() {
System.out.println("默认构造方法 Listene");
}
/**
* @see HttpSessionListener#sessionCreated(HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent se) {
synchronized (lock) {
alluser++;
}
}
/**
* @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent se) {
alluser--;
}
public static int getOnlineUserNum() {
return alluser;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.listener.SessionCountListener"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
在线人数为:<%=SessionCountListener.getOnlineUserNum() %>
</body>
</html>
过滤器Filter
拦截过程
案例登录+过滤器
下载地址
链接:https://pan.baidu.com/s/13qimmgcfCQ6YeWPjCHfaNA
提取码:7yvl
• 走读登录模块案例
• 1 session
• 2 拦截器
• 3 重定向 302
• response.sendRedirect(request.getContextPath() + “/page/index.jsp”);
springmvc入门
项目下载
链接:https://pan.baidu.com/s/1FfcbbzdiSFpcutt_a5BbvQ
提取码:n3zr
核心思想IOC控制反转
利用反射原理,把对象放到xml文件中让spring统一管理,实现解耦
spring对象管理xml方式
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class IocTest {
public static void main(String[] args) {
User user=new User();
//反射
try {
//解耦
Object object = Class.forName("com.testfan.ioc.User").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
//spring ioc
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//by name
User user2= (User) context.getBean("user");
//by type
user2= context.getBean(User.class);
System.out.println(user2);
System.out.println("carlist--"+user2.getCarList().size());
System.out.println("carSet--"+user2.getCarSet().size());
User user3= (User) context.getBean("user");
System.out.println(user2==user3);
System.out.println("car list"+ user2.getCar());
for (int i = 0; i < 100; i++) {
Singleton singleton=Singleton.getSingleton();
}
Car car = (Car) context.getBean("cartest");
System.out.println(car);
System.out.println("get user :"+ (car.getUser()==car.getUser2()));
Car car2 = (Car) context.getBean("car");
System.out.println(car2);
Singleton2 singleton2=Singleton2.getSingleton2();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="com.testfan.ioc.User" id="user" scope="singleton"> </bean>
</beans>
spring xml文件创建
填写完xml名字后,finish即可
spring对象管理包扫描
spring.xml文件配置扫描路径,然后在对象开头添加注解@Component,给对象起别名,使用时直接调对象别名,如果不起别名,使用时类名第一个字母小写
<context:component-scan base-package="com.testfan.spring" />
@Component("XXX别名")
spring对象管理依赖注入
1.通过注解方式
//主动注入对象 //byType
@Autowired
private UserBean userBean2;
//byname
@Resource(name = "userBean")
private UserBean userBean3;
2.通过xml方式
对象的使用:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class IocTest {
public static void main(String[] args) {
//对象解耦
ApplicationContext context =new ClassPathXmlApplicationContext("spring.xml");
//spring 对象管理第一种xml方式 <bean>
UserBean userBean=(UserBean) context.getBean("userBean");//通过name
// userBean=(UserBean) context.getBean(UserBean.class); //通过Type
System.out.println(userBean);
System.out.println("通过xml<bean>依赖注入:"+userBean.getCar());
System.out.println("listcar:"+userBean.getListCar().size());
System.out.println("setcar:"+userBean.getSetCar().size());
//spring对象管理第二种包扫描方式@compent
Car car=(Car) context.getBean("cartest");
System.out.println(car);
System.out.println("通过注解依赖注入"+car.getUserBean2());
System.out.println(car.getList());
}
}
springxml与配置文件集成
test. properties
name=\u5f20\u4e09\u0089
passwd=123456
num=101
list=topic1,topic2,topic3
carname=雪佛兰
<context:property-placeholder location="classpath*:test.properties" />
对象初始值来源于test. properties
单例模式
私有化构造方法,类以外不允许new,调用时只创建一次构造方法
//手写单例
public class Singleton {
private static Singleton singleton;
public static synchronized Singleton getSingleton() {
if(singleton==null) {
singleton =new Singleton();
}
return singleton;
}
private Singleton() {
System.out.println("create Singleton ");
}
public static void main(String[] args) {
Singleton singleton =new Singleton();
Singleton singleton2 =new Singleton();
}
}