-
Notifications
You must be signed in to change notification settings - Fork 18
Servlet
upan edited this page Oct 27, 2016
·
14 revisions
JavaEE 当前版本是7,正在进行8版本的开发。Servlet 是Java EE下的框架,不再JavaSE中。
目前项目中使用的是3.1.0版本
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
核心概念
- Servlet
- Filter
常用类:
- HttpServlet
- ServetConfig
- ServletContext
- Filter
- FilterConfig
- FilterChain
- RequestDispatcher
- HttpServletRequest
- HttpServletResponse
- HttpSession
- 一些 Listenser 类
Java Web 项目还需要一个非常重要的配置文件 web.xml ,每个 servlet 都必须在 web.xml 中定义并进行 URL 映射配置.
加载顺序: content-param --> listener --> filter --> servlet ,通过查看3.0版本的xsd可以看到大概有以下一级节点:
- context-param
- param-name
- param-value
- filter
- filter-name
- filter-class :The fully qualified classname of the filter
- async-supported
- init-param
- param-name
- param-value
- filter-mapping
- filter-name
- url-pattern
- dispatcher
- listener
- servlet
- servlet-mapping
- session-config
- mime-mapping
- welcome-file-list
- error-page
- jsp-config
- security-constraint
- login-config
- security-role
- message-destination
- locale-encoding-mapping-list
,配置示例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>helloFilter</filter-name>
<filter-class>demo.HelloFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>helloFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>hello_world</servlet-name>
<servlet-class>demo.HelloServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello_world</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>