Day11(线程安全、web开发技术之jstl、监听器、过滤器、springmvc入门对象管理方式:(xml、包扫描、依赖注入)、单例模式)

线程安全类

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4ghNvVW8-1590589395348)(Day11.assets/image-20200510150719139.png)]

分类

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GtLYWFzF-1590589395355)(Day11.assets/image-20200510150826091.png)]

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

拦截过程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qe6xdxno-1590589395360)(Day11.assets/image-20200510155915866.png)]

案例登录+过滤器

下载地址

链接: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文件创建

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A8a1QlSc-1590589395363)(Day11.assets/image-20200510170713683.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dZyzQq1R-1590589395366)(Day11.assets/image-20200510170739816.png)]

填写完xml名字后,finish即可

spring对象管理包扫描

spring.xml文件配置扫描路径,然后在对象开头添加注解@Component,给对象起别名,使用时直接调对象别名,如果不起别名,使用时类名第一个字母小写

<context:component-scan base-package="com.testfan.spring" />
@Component("XXX别名")

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-axatWNLI-1590589395368)(Day11.assets/image-20200510171142525.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eZopXINV-1590589395368)(Day11.assets/image-20200510171340712.png)]

spring对象管理依赖注入

1.通过注解方式

	//主动注入对象 //byType
	@Autowired  
	private UserBean userBean2;

	//byname
	@Resource(name = "userBean")
	private UserBean userBean3;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ADqhQjcT-1590589395369)(Day11.assets/image-20200511095528396.png)]

2.通过xml方式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-q9vkvNZt-1590589395371)(Day11.assets/image-20200511095651011.png)]

对象的使用:

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" />

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pdmQmFS0-1590589395372)(Day11.assets/image-20200511103425470.png)]

对象初始值来源于test. properties

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ch4XbQxF-1590589395373)(Day11.assets/image-20200511103646977.png)]

单例模式

私有化构造方法,类以外不允许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();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值