腊月的季节

Java web技术

Java web

Java作为一个编程语言,当然有他一套可以解决网络传输资源的机制。首先谈一下网站从何而来,其实要谈网站的由来,那么浏览器时必不可少的,可以这样说,浏览器就跟Java的虚拟机一样,只要你安装了这个虚拟机,就能运行HTML,JavaScript,CSS编写的网页,就如同别人给你Java代码你就能够在一台装了Java虚拟机的电脑上运行,道理是一样的,只不过浏览器解决的问题不一样,浏览器主要是供用户浏览信息,并能够得到他们想要的,主要就是信息共享,有信息提供方就有信息获取方,这就是浏览器解决的业务问题。他一点也不神奇。往编程说了去,就是你电脑发的数据包要经过提供方认证才能给你你想要的信息。

tomcat,jsp,httpserver,mysql

这四者是Java web的元老级了,是在SSH框架之前的所使用的,后面的SSH也是在他的基础上进行架构。主要包括src源码目录,library依赖包目录,webRoot下面的网页目录。接下来我要从SSH文件结构进行讲解。

入口配置文件web.xml

这个文件无论你是用老框架还是新框架这个都是web的入口。我们来看一个文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring4配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext*.xml</param-value>
</context-param>

<!--用来消除sitemesh 拦截器 Struts2拦截器的冲突 使之兼容 -->
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>

<filter>
<filter-name>sitemesh3</filter-name>
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>

<!-- hibernate4配置 -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>mySessionFactory</param-value>
</init-param>
</filter>

<!-- 配置struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter>
<filter-name>struts2_1</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- -->

<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>sitemesh3</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>struts2_1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

第一行就跟html的<!Doctype>作用差不多。
1、web.xml文件的作用
web.xml主要用来配置Filter、Listener、Servlet等。但是要说明的是web.xml并不是必须的,一个web工程可以没有web.xml
2、web容器的加载过程
web容器的加载顺序是:ServletContext->context-param->listener->filter->servlet。在web.xml文件中最好按照这种顺序配置这些元素。
3、web容器的启动过程
启动一个web项目的时候,web容器会去读取它的配置文件web.xml,读取listener和context-param两个结点。
紧接着,容器创建一个ServletContext上下文,这个web项目的所有部分都将共享这个上下文。
容器将context-param转换为键值对,并交给servletContext。
容器创建listener中的类实例,创建监听器。

web-app根元素

web.xml的模式文件是由Sun公司定义的,每个web.xml文件的根元素中,都必须标明这个 web.xml使用的是哪个模式文件。其它的元素都放在之中,是根节点。

1
2
3
4
5
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
</web-app>

icon web应用图标

1
2
3
4
<icon>
<small-icon>/images/app_small.gif</small-icon>
<large-icon>/images/app_large.gif</large-icon>
</icon>

display-name web应用名称

1
<display-name>Tomcat Example</display-name>

discription web应用描述

1
<discription>Tomcat Example servlets and jsp pages</discription>

context-param 上下文参数

声明应用范围内的初始化参数。它用于向 ServletContext提供键值对,即应用程序上下文信息。我们的listener, filter等在初始化时会用到这些上下文中的信息。在servlet里面可以通过getServletContext().getInitParameter(“context/param”)得到。

1
2
3
4
5
<context-param>
<param-name>ContextParameter</para-name>
<param-value>test</param-value>
<description>It is a test parameter.</description>
</context-param>

filter过滤器

1
2
3
4
5
6
7
8
9
10
11
12
<filter>
<filter-name>setCharacterEncoding</filter-name>
<filter-class>com.myTest.setCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

listener监听器

1
2
3
<listener> 
<listener-class>com.listener.SessionListener</listener-class>
</listener>

servlet

servlet 用来声明一个servlet的数据,主要有以下子元素:
servlet-name 指定servlet的名称
servlet-class 指定servlet的类名称
jsp-file 指定web站台中的某个JSP网页的完整路径
init-param 用来定义参数,可有多个init-param。
load-on-startup 当值为正数或零时,从小到大加载。否则第一次访问时加载。
servlet-mapping 用来定义servlet所对应的URL,包含两个子元素
servlet-name 指定servlet的名称
url-pattern 指定servlet所对应的URL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!-- 基本配置 -->
<servlet>
<servlet-name>snoop</servlet-name>
<servlet-class>SnoopServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>snoop</servlet-name>
<url-pattern>/snoop</url-pattern>
</servlet-mapping>
<!-- 高级配置 -->
<servlet>
<servlet-name>snoop</servlet-name>
<servlet-class>SnoopServlet</servlet-class>
<init-param>
<param-name>foo</param-name>
<param-value>bar</param-value>
</init-param>
<run-as>
<description>Security role for anonymous access</description>
<role-name>tomcat</role-name>
</run-as>
</servlet>
<servlet-mapping>
<servlet-name>snoop</servlet-name>
<url-pattern>/snoop</url-pattern>
</servlet-mapping>

session-config会话超时配置

1
2
3
<session-config>
<session-timeout>120</session-timeout><!-- 单位为分钟 -->
</session-config>

mime-mapping

1
2
3
4
<mime-mapping>
<extension>htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>

welcome-file-list欢迎文件页

1
2
3
4
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

error-page错误页面

1
2
3
4
5
6
7
8
9
10
<!-- 1、通过错误码来配置error-page。当系统发生×××错误时,跳转到错误处理页面。 -->
<error-page>
<error-code>404</error-code>
<location>/NotFound.jsp</location>
</error-page>
<!-- 2、通过异常的类型配置error-page。(即空指针异常)时,跳转到错误处理页面。 -->
<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/error.jsp</location>
</error-page>

jsp-config 设置jsp

1
2
3
4
5
6
7
8
9
10
<jsp-config>            包括 <taglib><jsp-property-group>两个子元素。其中<taglib>元素在JSP1.2时就已经存在;而<jsp-property-group> 是JSP 2.0 新增的元素。
<jsp-property-group> 元素主要有八个子元素,它们分别为:
<description> 设定的说明
<display-name> 设定名称
<url-pattern> 设定值所影响的范围,如: /CH2 或 /*.jsp
<el-ignored> 若为 true,表示不支持 EL 语法
<scripting-invalid> 若为 true,表示不支持 <% scripting %>语法
<page-encoding> 设定 JSP 网页的编码
<include-prelude> 设置 JSP 网页的抬头,扩展名为 .jspf
<include-coda> 设置 JSP 网页的结尾,扩展名为 .jspf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<jsp-config>
<taglib>
<taglib-uri>Taglib</taglib-uri>
<taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>
<jsp-property-group>
<description>Special property group for JSP Configuration JSP example.</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>/jsp/* </url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>GB2312</page-encoding>
<scripting-invalid>true</scripting-invalid>
<include-prelude>/include/prelude.jspf</include-prelude>
<include-coda>/include/coda.jspf</include-coda>
</jsp-property-group>
</jsp-config>

热评文章