一. 登录验证码及校验
@WebServlet("/demo")
public class DemoServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//输出文字流
//PrintWriter out = resp.getWriter();
//获取响应流
ServletOutputStream os = resp.getOutputStream();
InputStream is = new FileInputStream(new File(getServletContext().getRealPath("images"),"a.png"));
int index = -1;
while((index=is.read())!=-1){
os.write(index);
}
}
}
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private UsersService usersService;
@Override
public void init() throws ServletException {
ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
usersService=ac.getBean("usersService",UsersServiceImpl.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String code = req.getParameter("code");
String codeSession = req.getSession().getAttribute("code").toString();
if(codeSession.equals(code)){
String username = req.getParameter("username");
String password = req.getParameter("password");
Users users = new Users();
users.setPassword(password);
users.setUsername(username);
Users user = usersService.login(users);
if(user!=null){
resp.sendRedirect("main.jsp");
}else{
req.setAttribute("error", "用户名密码不正确");
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}else{
req.setAttribute("error", "验证码不正确");
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
}
@WebServlet("/validcode")
public class ValidCodeServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建一张图片
//单位:像素
BufferedImage image = new BufferedImage(200, 100, BufferedImage.TYPE_INT_RGB);
//透明的玻璃
//向画板上画内容之前必须先设置画笔.
Graphics2D gra = image.createGraphics();
gra.setColor(Color.WHITE);
//从哪个坐标开始填充, 后两个参数,矩形区域
gra.fillRect(0, 0, 200, 100);
List<Integer> randList = new ArrayList<Integer>();
Random random =new Random();
for (int i = 0 ;i<4;i++) {
randList.add(random.nextInt(10));
}
//设置字体
gra.setFont(new Font("宋体",Font.ITALIC|Font.BOLD,40));
Color[] colors = new Color[]{Color.RED,Color.YELLOW,Color.BLUE,Color.GREEN,Color.PINK,Color.GRAY};
for (int i = 0; i < randList.size(); i++) {
gra.setColor(colors[random.nextInt(colors.length)]);
gra.drawString(randList.get(i)+"", i*40, 70+(random.nextInt(21)-10));
}
for (int i = 0; i < 2; i++) {
gra.setColor(colors[random.nextInt(colors.length)]);
//画横线
gra.drawLine(0, random.nextInt(101), 200, random.nextInt(101));
}
ServletOutputStream outputStream = resp.getOutputStream();
//工具类
ImageIO.write(image, "jpg", outputStream);
//把验证码放入到session中
HttpSession session = req.getSession();
session.setAttribute("code", ""+randList.get(0)+randList.get(1)+randList.get(2)+randList.get(3));
}
}
二.AOP
中文名称,面向切面编程。英文名称,Aspect Oriented Programming。正常程序执行流程都是纵向执行流程,又叫面向切面编程,在原有纵向执行流程中添加切面,不需要修改原有程序代码(体现出程序高扩展性),原有功能相当于释放了部分逻辑,让职责更加明确。
面向切面编程是什么?在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面过程就叫做面向切面编程。
常用概念:原有功能,切点,pointcut。前置通知,在切点之前执行的功能,before advice。后置通知,在切点之后执行的功能,after advice。如果切点执行过程中出现异常,会触发异常通知,throws advice。所有功能总称叫做切面。织入,把切面嵌入到原有功能的过程叫做织入。
spring提供了2种AOP实现方式:①Schema-based,每个通知都需要实现接口或类,配置spring配置文件时在<aop:config>配置。②AspectJ,每个通知不需要实现接口或类,配置spring配置文件是在<aop:config>的子标签<aop:aspect>中配置。
Schema-based方式
实现步骤:①导入jar包。②新建通知类。新建前置通知,arg0切点方法对象Method对象,arg1切点方法参数,arg2切点在哪个对象中;新建后置通知类,arg0切点方法返回值,arg1切点方法对象,arg2切点方法参数,arg3切点方法所在类的对象。配置spring配置文件,引入aop命名空间,配置通知类的<bean>,配置切面,*通配符匹配任意方法名任意类名任意一级包名,如果希望匹配任意方法参数(..)。编写测试代码运行。
public class MyBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("执行前置通知");
}
}
public class MyAfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("执行后置通知");
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/sc hema/beans
http://www.springframework.org/schema/beans/spring-be ans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop. xsd">
<!-- 配置通知类对象,在切面中引入 -->
<bean id="mybefore" class="com.bjsxt.advice.MyBeforeAdvice"></bean>
<bean id="myafter" class="com.bjsxt.advice.MyAfterAdvice"></bean>
<!-- 配置切面 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo2())" id="mypoint"/>
<!-- 拦截任意模块下的任意类的任一方法 -->
<aop:pointcut expression="execution(* com.bjsxt.*.test.*.*())" id="mypoint"/>
<!-- 通知 -->
<aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/>
<aop:advisor advice-ref="myafter" pointcut-ref="mypoint"/>
</aop:config>
<!-- 配置 Demo 类,测试使用 -->
<bean id="demo" class="com.bjsxt.test.Demo"></bean>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Demo demo = ac.getBean("demo",Demo.class);
demo.demo1();
demo.demo2();
demo.demo3();
}
}
配置异常通知(AspectJ方式):只有当切点报异常才能触发异常通知。在spring中只有AspectJ方式提供了异常通知的方法,如果希望通过schema-base实现需要按照特定的要求自己编写方法。
实现步骤:①新建类,在类中写任意名称的方法。②在spring配置文件中配置,<aop:aspect>的ref属性表示方法在哪个类中,<aop:xxxx/>表示什么通知,method当触发这个通知时调用哪个方法,throwing异常对象名必须和通知方法参数名相同(可以不在通知中声明异常对象)。
public class MyThrowAdvice{
public void myexception(Exception e1){
System.out.println("执行异常通知"+e1.getMessage());
}
}
<bean id="mythrow" class="com.bjsxt.advice.MyThrowAdvice"></bean>
<aop:config>
<aop:aspect ref="mythrow">
<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1())" id="mypoint"/>
<aop:after-throwing method="myexception" pointcut-ref="mypoint" throwing="e1"/>
</aop:aspect>
</aop:config>
<bean id="demo" class="com.bjsxt.test.Demo"></bean>
异常通知(Schema-based方式):①新建一个类实现throwsAsvice接口,必须自己写方法且必须叫afterThrowing,有两种参数方式必须是1个或者4个,异常类型要与切点报的异常类型一致。②在ApplicationContext.xml配置。
public class MyThrow implements ThrowsAdvice{
public void afterThrowing(Method m, Object[] args, Object target, Exception ex) {
System.out.println("执行异常通知");
}
public void afterThrowing(Exception ex) throws Throwable {
System.out.println("执行异常通过-schema-base 方式");
}
}
<bean id="mythrow" class="com.bjsxt.advice.MyThrow"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1())" id="mypoint"/>
<aop:advisor advice-ref="mythrow" pointcut-ref="mypoint" />
</aop:config>
<bean id="demo" class="com.bjsxt.test.Demo"></bean>
环绕通知(Schema-based方式):把前置通知和后置通知都写到一个通知中,组成了环绕通知。
实现步骤:①新建一个类实现MethodInterceptor。②配置applicationContext.xml。
public class MyArround implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("环绕-前置");
Object result = arg0.proceed();//放行,调用切点方式
System.out.println("环绕-后置");
return result;
}
}
<bean id="myarround" class="com.bjsxt.advice.MyArround"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1())" id="mypoint"/>
<aop:advisor advice-ref="myarround" pointcut-ref="mypoint" />
</aop:config>
<bean id="demo" class="com.bjsxt.test.Demo"></bean>
使用AspectJ方式实现
①新建类,不用实现。类中方法名任意。②配置spring文件。<aop:after/>后置通知,是否出现异常都执行;<aop:after-returing/>后置通知,只有当切点正确执行时执行;<aop:after/>和<aop:after-returing/>和<aop:after-throwing/>执行顺序和配置顺序有关;execution()括号不能扩上args;中间使用and不能使用&&由spring把and解析成&&;args(名称)名称自定义的。顺序和demo1(参数,参数)对应;<aop:before/>arg-names="名称"名称来源于expression=""中args(),名称必须一样;args()有几个参数,args-names里面必须有几个参数;arg-names=""里面名称必须和通知方法参数名对应。
public class MyAdvice {
public void mybefore(String name1,int age1){
System.out.println("前置"+name1);
}
public void mybefore1(String name1){
System.out.println("前置:"+name1);
}
public void myaftering(){
System.out.println("后置 2");
}
public void myafter(){
System.out.println("后置 1");
}
public void mythrow(){
System.out.println("异常");
}
public Object myarround(ProceedingJoinPoint p) throws Throwable{
System.out.println("执行环绕");
System.out.println("环绕-前置");
Object result = p.proceed();
System.out.println("环绕后置");
return result;
}
}
<aop:config>
<aop:aspect ref="myadvice">
<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1(String,int)) and args(name1,age1)" id="mypoint"/>
<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1(String)) and args(name1)" id="mypoint1"/>
<aop:before method="mybefore" pointcut-ref="mypoint" arg-names="name1,age1"/>
<aop:before method="mybefore1" pointcut-ref="mypoint1" arg-names="name1"/>
<aop:after method="myafter" pointcut-ref="mypoint"/>
<aop:after-returning method="myaftering" pointcut-ref="mypoint"/>
<aop:after-throwing method="mythrow" pointcut-ref="mypoint"/>
<aop:around method="myarround" pointcut-ref="mypoint"/>
</aop:aspect>
</aop:config>
使用注解(基于Aspect):spring不会自动去寻找注解,必须告诉spring中可能有注解,需要引入xmlns:context。
@Component,相当于<bean/>,如果没有参数把类名首字母变小写,相当于<bean id=""/>,@Component("自定义名称")。
实现步骤:①在spring配置文件中设置注解在哪些包中。②在Demo类中添加@Componet,在方法上添加@Pointcut("")定义切点。③在通知类中配置,@Component类被spring管理,@Aspect相当于<aop:aspect/>表示通知方法在当前类中。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop. xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:component-scan base-package="com.bjsxt.advice,com.bjsxt.test"></context:component-scan>
@Component
public class Demo {
@Pointcut("execution(* com.bjsxt.test.Demo.demo1())")
public void demo1() throws Exception{
//int i = 5/0;
System.out.println("demo1");
}
}
@Component
@Aspect
public class MyAdvice {
@Before("com.bjsxt.test.Demo.demo1()")
public void mybefore(){
System.out.println("前置");
}
@After("com.bjsxt.test.Demo.demo1()")
public void myafter(){
System.out.println("后置通知");
}
@AfterThrowing("com.bjsxt.test.Demo.demo1()")
public void mythrow(){
System.out.println("异常通知");
}
@Around("com.bjsxt.test.Demo.demo1()")
public Object myarround(ProceedingJoinPoint p) throws
Throwable{
System.out.println("环绕-前置");
Object result = p.proceed(); System.out.println("环绕-后置");
return result;
}
}
三. 代理设计模式
设计模式:前人总结的一套解决特定问题的代码。
代理设计模式的有点:保护真实对象,让真实对象职责更明确,扩展。、
代理设计模式:真实对象(老总),代理对象(秘书),抽象对象(抽象功能),谈小目标。
静态代理设计模式
由代理对象代理所有真实对象的功能,自己编写代理类,每个代理的功能需要单独编写。静态代理设计模式的缺点,当代理功能比较多时,代理类中方法需要写很多。
public class MiShu implements Gongneng{
private Laozong laozong = new Laozong("老总");
@Override
public void chifan() {
System.out.println("约定时间");
laozong.chifan();
System.out.println("把访客信息备注");
}
}
public class Women {
public static void main(String[] args) {
MiShu mishu = new MiShu();
mishu.chifan();
}
}
动态代理:为了解决静态代理频繁编写代理功能缺点。分类,JDK提供的,cglib动态代理。
JDK动态代理
和cglib动态代理对比,优点是jdk自带,不需要额外的导入jar包。缺点是真实对象必须实现接口,利用反射机制效率不高。
使用JDK动态代理时可能出现下面异常,出现原因是希望把接口对象转换为具体真实对象。
public class Mishu implements InvocationHandler {
private Laozong laozong=new Laozong() ;
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("预约时间");
Object result = method.invoke(laozong, args);
System.out.println("记录访客信息");
return result;
}
}
public class Women {
public static void main(String[] args) {
System.out.println(Women.class.getClassLoader()==Laozong.class.getClassLoader());
Mishu mishu = new Mishu();
//第一个参数:反射时使用的类加载器
//第二个参数:Proxy需要实现什么接口
//第三个参数:通过接口对象调用方法时,需要调用哪个类的invoke方法
Gongneng gongneng = (Gongneng) Proxy.newProxyInstance(Women.class.getClassLoader(), new Class[]{Gongneng.class}, mishu);
Laozong laozong = (Laozong) gongneng;
gongneng.chifan();
}
}
cglib动态代理
cglib优点,基于字节码,生成真实对象的子类,运行效率高于JDK动态代理,不需要实现接口。cglib缺点,非JDK功能,需要额外导入jar包。使用spring aop时,只要出现Proxy和真实对象转换异常,设置为true使用cglib,设置为false使用jdk(默认值)。
public class Mishu implements MethodInterceptor{
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
System.out.println("预约时间");
//invoke()调用子类重写的方法
//arg1.invoke(arg0, arg2);
//arg3.invokeSuper(arg0, arg2);
Object result = arg3.invokeSuper(arg0, arg2);
System.out.println("备注");
return result;
}
}
public class Women {
public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Laozong.class);
enhancer.setCallback(new Mishu());
Laozong laozong = (Laozong) enhancer.create();
laozong.chifan();
}
}
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>