config可以读取web.xml中的配置内容
在WEB-INF文件下的文件是不对外可见的,所以要想访问放在WEB-INF文件夹中的内容,则就需要对文件进行映射,使用一个名字来映射WEB-INF/hello.jsp,而要想完成这样映射,则就要必须在web.xml文件中编写代码。
修改web.xml加入以下内容:
<servlet>
<servlet-name>demo</servlet-name>
<jsp-file>/WEB-INF/hello.jsp</jsp-file>
<init-param>
<param-name>driver</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:oracle:thin:@localhost:1521:MLDN</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/hello.lxh</url-pattern>
</servlet-mapping>
在hello.jsp中的代码如下:config读取web.xml中的信息。
<html>
<head>
<title>HELLO JSP WORLD!!!</title>
</head>
<body>
<h1><%=config.getInitParameter("driver")%></h1>
<h1><%=config.getInitParameter("url")%></h1>
</body>
</html>
在web.xml.中配置默认页,和出错页:
<welcome-file-list>
<welcome-file>test.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/error.htm</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.htm</location>
</error-page>