目录
1servlet 和 filter 的异同
2 内存中的字符编码
3 gbk和utf-8的特点
4 struts和spring的整合
5 struts和spring的整合步骤
6 springIOC在action类中的使用
7 注解回顾
1 servlet 和 filter 的异同
1.1 相同点
filter完全可以代替servlet使用,但是filter中的某些功能servlet不能实现,而servlet的所有功能filter都能实现
1.2 不同点
1.2.1 概念上
servlet 是运行在服务器端的程序,动态生成web页面
filter 是一段可以复用的代码,不能生成请求和响应,但是可以对相应和请求做相应的处理
1.2.2 生命周期上
servlet 是在web服务器启动或web服务器接收到第一次请求时利用构造器实例化servlet,然后调用init方法进行初始化,之后的每次请求会调用doGet或者doPost方法进行处理;当关闭服务器的时候,调用destroy方法销毁实例
filter 是在web服务器启动时利用构造器实例化filter,然后调用init方法进行初始化,之后的每次请求调用doFilter方法进行处理,关闭服务器的时候,调用destroy方法销毁实例
1.2.3 类型不同
servlet是类、filter是接口
1.3 servlet 、filter的编程实现
1 packagecn.xiangxu;2
3 importjava.io.IOException;4 importjava.io.PrintWriter;5
6 importjavax.servlet.ServletException;7 importjavax.servlet.http.HttpServlet;8 importjavax.servlet.http.HttpServletRequest;9 importjavax.servlet.http.HttpServletResponse;10
11 /**
12 * Servlet implementation class DemoServlet13 */
14 public class DemoServlet extendsHttpServlet {15 private static final long serialVersionUID = 1L;16
17 /**
18 *@seeHttpServlet#HttpServlet()19 */
20 publicDemoServlet() {21 super();22 System.out.println("01 利用构造器实例化");23 }24
25 @Override26 public void init() throwsServletException {27 super.init();28 System.out.println("02 利用init方法初始化");29 }30
31 /**
32 *@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)33 */
34 protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {35 System.out.println("03 调用处理方法");36
37 response.setContentType("text/html;charset:utf-8"); //设置消息头部分信息
38 response.setCharacterEncoding("utf-8");39
40 PrintWriter out =response.getWriter();41 out.println("hello servlet");42 out.close();43 }44
45 @Override46 public voiddestroy() {47 super.destroy();48 System.out.println("04 利用destroy方法销毁");49 }50
51 /**
52 *@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)53 */
54 protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {55 //TODO Auto-generated method stub
56 doGet(request, response);57 }58
59 }
DemoServlet
1 packagecn.xiangxu;2
3 importjava.io.IOException;4 importjava.io.PrintWriter;5
6 importjavax.servlet.Filter;7 importjavax.servlet.FilterChain;8 importjavax.servlet.FilterConfig;9 importjavax.servlet.ServletException;10 importjavax.servlet.ServletRequest;11 importjavax.servlet.ServletResponse;12 importjavax.servlet.http.HttpServletResponse;13
14 /**
15 * Servlet Filter implementation class DemoFilter16 */
17 public class DemoFilter implementsFilter {18
19 /**
20 * Default constructor.21 */
22 publicDemoFilter() {23 System.out.println("01 利用构造器实例化");24 }25
26 /**
27 *@seeFilter#init(FilterConfig)28 */
29 public void init(FilterConfig fConfig) throwsServletException {30 System.out.println("02 利用init方法初始化");31 }32
33 /**
34 *@seeFilter#doFilter(ServletRequest, ServletResponse, FilterChain)35 */
36 public void doFilter(ServletRequest request, ServletResponse resp, FilterChain chain) throwsIOException, ServletException {37 System.out.println("03 调用处理方法");38
39 HttpServletResponse response =(HttpServletResponse)resp;40
41 response.setContentType("text/html;charset:utf-8");42 response.setCharacterEncoding("utf-8");43
44 PrintWriter out =response.getWriter();45 out.println("hello filter");46 out.close();47 }48
49 /**
50 *@seeFilter#destroy()51 */
52 public voiddestroy() {53 System.out.println("04 利用destroy方法销毁");54 }55
56 }
DemoFilter
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 servletVsFilter
4
5 index.html
6 index.htm
7 index.jsp
8 default.html
9 default.htm
10 default.jsp
11
12
13 servlet
14 cn.xiangxu.DemoServlet
15
16
17 servlet
18 /servlet.do
19
20
21
22 filter
23 cn.xiangxu.DemoFilter
24
25
26 filter
27 /filter.do
28
29
web.xml
项目结构图
servlet效果图
filter效果图
2 为什么对内存中的字符进行编码
字符在服务器内存中是16位的char,网络传输的单位是8为的byte,我们必须对数据进行拆分才能够进行传输,拆分的过程我们叫做编码
3 描述一下GBK和UTF-8的特点
utf-8是国际化的最优编码方案,包括了所有的字符,中文占3字节
gbk是本土化的最优编码方案,包含的字符较少,中文占2字节
4 struts2 和 spring 整合
4.1 为什么要进行整合
使用spring框架管理组件,实现注入,简化代码
4.2 怎么整合
导包(struts2 - spring - plugin)
配置文件(web.xml spring-struts.xml)
struts.xml配置文件
5 利用 struts + spring 实现 hello world 案例
5.1 导包
struts2核心包、struts和spring的整合插件包
1
2 4.0.0
3 cn.xiangxu
4 ssh02
5 0.0.1-SNAPSHOT
6 war
7
8
9 org.apache.struts
10 struts2-core
11 2.3.8
12
13
14 org.apache.struts
15 struts2-spring-plugin
16 2.3.8
17
18
19
maven依赖文件代码
5.2 配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 ssh02
4
5 index.html
6 index.htm
7 index.jsp
8 default.html
9 default.htm
10 default.jsp
11
12
13
15
16 org.springframework.web.context.ContextLoaderListener
17
18
19
20
21 contextConfigLocation
22 classpath:spring_*.xml
23
24
25
26
27 mvc
28 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
29
30
31 mvc
32 /*33 34
35
36
web.xml配置文件源代码
5.2.1 配置spring监听
所需类的位置
5.2.2 配置Spring配置文件的位置
配置文件所处位置
5.2.3 配置主控制器和过滤条件
所需类的位置
5.3 配置spring_context.xml
只需要在里面配置组件扫描即可
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee"
5 xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"
6 xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:util="http://www.springframework.org/schema/util"
7 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
8 xsi:schemaLocation="9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd10 http://www.springframework.org/schema/context/spring-context-3.0.xsd11 http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd12 http://www.springframework.org/schema/jee/spring-jee-3.0.xsd13 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd14 http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd15 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd16 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd17 http://www.springframework.org/schema/util/spring-util-3.0.xsd">
18
19
20
21
22
spring配置文件代码
5.4 配置struts.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"4 "http://struts.apache.org/dtds/struts-2.3.dtd">
5
6
7
8
9
10 /WEB-INF/jsp/msg.jsp11
12
13
14
15
struts配置文件
5.5 编写action类
1 packagecn.xiangxu;2
3 public classTestAction {4 publicString execute() {5 System.out.println("struts整合spring");6 return "success";7 }8 }
action类代码
5.6 编写所需的JSP文件
1
2 pageEncoding="utf-8"%>
3
4
5
6
7
test8
9
10
恭喜你,struts整合spring成功!
11
12
jsp文件代码
项目结构图
6 struts整合spring后就可以使用springIOC啦
例如:持久层的action类中通过依赖注入服务层的类,从而就可以使用服务层的相关属性和方法啦
6.1 案例改进所需的业务层类
1 packagecn.xiangxu.service;2
3 importorg.springframework.stereotype.Service;4
5 @Service6 public classTestService {7 public voidtest() {8 System.out.println("我是业务层的数据哟。");9 }10 }
增加的service类代码
6.2 案例改进一
在action类上添加@Controller注解,这样spring容器就会自动给其增加一个bean
在action类上添加@Scope("prototype")注解,这样就能让每一个请求都有一个action处理类,如果不写就是所有的请求公用一个action处理类(即:默认时使用单例模式)
在action类中依赖注入业务层,从而实现控制反转
1 packagecn.xiangxu.action;2
3 importjavax.annotation.Resource;4
5 importorg.springframework.context.annotation.Scope;6 importorg.springframework.stereotype.Controller;7
8 importcn.xiangxu.service.TestService;9
10 @Controller11 @Scope("prototype")12 public classTestAction {13
14 @Resource(name="testService")15 privateTestService testService;16
17 publicString execute() {18 System.out.println("struts整合spring");19 testService.test();20 return "success";21 }22 }
action类改进
6.3 案例改进二
struts中的action标签中的class属性值可以用相应bean的id代替
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"4 "http://struts.apache.org/dtds/struts-2.3.dtd">
5
6
7
8
9
10 /WEB-INF/jsp/msg.jsp11
12
13
14
15
sturts配置文件代码改进
7 注解回顾
@Component@Controller@Service@Repository
@Resource@Scope
@Component通用的标记(不推荐使用,菜鸟可以使用,【AOP编程时可以使用】)
格式1:@Controller("指定id")
格式2:@Controller 这种方式时使用默认的id,即:类名小写后的值
@Controller作用于控制层
@Service作用于业务层
@Repository作用于持久层
@Scope避免线程的安全问题,指定值为“prototype",这样每个请求都会对应一个action处理类;如果不写的话就是单例模式(即:所有请求对应一个action处理类)
格式:@Scope("prototype")
@Resource依赖注入时的声明
格式:@Resource(name="依赖注入的id")