web-app
根节点
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
……
……
……
</web-app>
welcome-file-list
功能:定义首页
welcome-file可以有多个,从上往下依次寻找第一个存在的首页面
<welcome-file-list>
<welcome-file>首页面</welcome-file>
</welcome-file-list>
context-param
功能:配置全局初始化参数
<context-param>
<param-name>参数名1</param-name>
<param-value>参数值1</param-value>
</context-param>
<context-param>
<param-name>参数名2</param-name>
<param-value>参数值2</param-value>
</context-param>
servlet(@WebServlet)
功能:配置Servlet的访问路径,初始化参数(可有多个)
配置servlet和@WebServlet功能一样,但是配置Servlet比@WebServlet更加安全
<servlet>
<servlet-name>Demo1Servlet</servlet-name>
<servlet-class>包名.类名</servlet-class>
<init-param>
<param-name>参数名1</param-name>
<param-value>参数值1</param-value>
</init-param>
<init-param>
<param-name>参数名2</param-name>
<param-value>参数值2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Demo1Servlet</servlet-name>
<url-pattern>访问地址</url-pattern>
</servlet-mapping>
初始化参数的读取
private String encoding; //Servlet的局部初始化参数
private String encode; //全局初始化参数
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config); //公有
encoding = config.getInitParameter("encoding"); //局部
encode = getServletContext().getInitParameter("encode"); //全局
}
JSP
功能:配置JSP的访问路径
<servlet>
<servlet-name>变量名</servlet-name>
<jsp-file>/文件名</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>变量名</servlet-name>
<url-pattern>/访问地址</url-pattern>
</servlet-mapping>
错误
<error-page>
<error-code>网页异常码</error-code>
<location>/跳转到的页面</location>
</error-page>
<error-page>
<exception-type>java异常类型</exception-type>
<location>/跳转到的页面</location>
</error-page>