B/S下的相关概念介绍
B/S:浏览器、服务器
B/S是如何通信的呢?
- socket
- web容器(http协议)—tomcat
去封装了socket,实现了浏览器和服务器直接的通信,程序员不需要自己去进行socket编程了 - tomcat:web容器,Servlet容器
tomcat说了,我只会调用Servlet接口的实现类代码。 - Servlet接口是JavaEE推出的web编程的服务器程序的标准接口。
Servlet的实例,是由tomcat来创建的,是由程序员来编写的代码。 - Servlet对象什么时候被创建呢?
部署描述符:web.xml(描述了Servlet实现类是哪一个,对于Servlet可以指定创建实例的时机) - 默认Servlet实例是第一次被访问的时候创建,只会存在一个实例
可以通过设置,完成tomcat启动的时候,就创建一个Servlet实例
Servlet接口的生命周期方法有哪些?
init()
- 如果存在Servlet实例,则开始其初始化阶段,执行器初始化方法(init()方法
destroy
- 最后是销毁阶段,程序结束或者是服务器停止,调用其销毁方法(destroy()方法)
service(处理B/S架构下的请求的)
- 第三阶段是响应客户端请求阶段,调用service()方法,根据提交方式选择执行doGet()方法或者doPost()方法
Servlet的创建时机
- 默认Servlet实例是第一次被访问的时候创建,只会存在一个实例
- 可以通过设置,完成tomcat启动的时候,就创建一个Servlet实例
Servlet由谁创建
- 是由tomcat来创建的,是由程序员来编写的代码
思考Servlet程序如何通过一个Servlet去接收所有的Servlet请求,然后分别调用不同的处理代码去处理?????
url-pattern的匹配问题
见下图
手写springmvc1.0版本
需求:
- 访问http://localhost/queryUser?id=1&name=james
- 访问http://localhost/addUser
- 简单分析Servlet实现以上代码的弊端
如果url-pattern没有匹配成后面三种方式的话,那么一个Servlet只能处理一个请求,当请求很多时,会编写很多Servlet类。 - 首先将url-pattern改为/,这样就可以拦截所有的Servlet请求。
- url-pattern为/时,对应的Servlet类应该如何设计才不会臃肿或者说扩展性不好?
- 一种是编写一个基类(BaseServlet),再编写不同的子类去实现该基类
http://localhost/userServlet?method=query&id=1&name=james - 另一种就是编写一个单独的Servlet类去只负责接收请求,分发请求、响应结果,不需要编写其他的Servlet类继承于它
springmvc中采用适配器模式来实现,见下图–springmvc处理请求的流程。
适配器模式
适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的
类的兼容性问题。主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。
类的适配器
核心思想就是:有一个 Source 类,拥有一个方法,待适配,目标接口是 Targetable ,通过 Adapter类,将 Source 的功能扩展到 Targetable 里,看代码:
public class Source {
public void method1() {
System.out.println("this is original method!");
}
}
public interface Targetable {
/* 与原类中的方法相同 */
public void method1();
/* 新类的方法 */
public void method2();
}
public class Adapter extends Source implements Targetable {
@Override
public void method2() {
System.out.println("this is the targetable method!");
}
}
Adapter 类继承 Source 类,实现 Targetable 接口,下面是测试类:
public class AdapterTest {
public static void main(String[] args) {
Targetable target = new Adapter();
target.method1();
target.method2();
}
}
这样 Targetable 接口的实现类就具有了 Source 类的功能。
对象的适配器模式
基本思路和类的适配器模式相同,只是将 Adapter 类作修改,这次不继承 Source 类,而是持有Source 类的实例,以达到解决兼容性的问题。看图:
只需要修改 Adapter 类的源码即可:
public class Wrapper implements Targetable {
private Source source;
public Wrapper(Source source){
super();
this.source = source;
}
@Override
public void method2() {
System.out.println("this is the targetable method!");
}
@Override
public void method1() {
source.method1();
}
}
测试类:
public class AdapterTest {
public static void main(String[] args) {
Source source = new Source();
Targetable target = new Wrapper(source);
target.method1();
target.method2();
}
}
输出与第一种一样,只是适配的方法不同而已。
接口的适配器模式
第三种适配器模式是接口的适配器模式,接口的适配器是这样的:有时我们写的一个接口中有多个抽象方法,当我们写该接口的实现类时,必须实现该接口的所有方法,这明显有时比较浪费,因为并不是所有的方法都是我们需要的,有时只需要某一些,此处为了解决这个问题,我们引入了接口的适配器模式,借助于一个抽象类,该抽象类实现了该接口,实现了所有的方法,而我们不和原始的接口打交道,只和该抽象类取得联系,所以我们写一个类,继承该抽象类,重写我们需要的方法就行。看一下类图:
这个很好理解,在实际开发中,我们也常会遇到这种接口中定义了太多的方法,以致于有时我们在一些实现类中并不是都需要。
看代码:
public interface Sourceable {
public void method1();
public void method2();
}
抽象类Wrapper:
public abstract class Wrapper implements Sourceable{
public void method1(){}
public void method2(){}
}
public class SourceSub1 extends Wrapper {
public void method1(){
System.out.println("the sourceable interface's first Sub1!");
}
}
public class SourceSub2 extends Wrapper {
public void method2(){
System.out.println("the sourceable interface's second Sub2!");
}
}
测试类:
public class WrapperTest {
public static void main(String[] args) {
Sourceable source1 = new SourceSub1();
Sourceable source2 = new SourceSub2();
source1.method1();
source1.method2();
source2.method1();
source2.method2();
}
}
测试输出:
the sourceable interface’s first Sub1!
the sourceable interface’s second Sub2!
达到了我们的效果!
讲了这么多,总结一下三种适配器模式的应用场景:
- 类的适配器模式:当希望将一个类转换成满足另一个新接口的类时,可以使用类的适配器模式,创建一个新类,继承原有的类,实现新的接口即可。
- 对象的适配器模式:当希望将一个对象转换成满足另一个新接口的对象时,可以创建一个Wrapper类,持有原类的一个实例,在Wrapper类的方法中,调用实例的方法就行。
- 接口的适配器模式:当不希望实现一个接口中所有的方法时,可以创建一个抽象类Wrapper,实现所有方法,我们写别的类的时候,继承抽象类即可。