Servlet配置包含Servlet的名字、Servlet的类(如果是JSP,那么就指定JSP文件)、初始化参数、启动装入的优先级、Servlet的映射和运行的安全设置。Servlet配置的DTD如下
引用
<!EKENENT servlet(icon?,servlet-name,display-name?,description?,
(servlet-class|jsp-file),init-param*,load-on-startup?,run-as?,security-role-ref*)>
(servlet-class|jsp-file),init-param*,load-on-startup?,run-as?,security-role-ref*)>
下面结合一个实例
- package com.test.ch8
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- publicclass CounterServlet extends HttpServlet{
- int counter=0;
- int org=0;
- //处理doGet请求
- publicvoid doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
- response.setContentType("text/html;charset=gb2312");
- PrintWriter out=response.getWriter();
- counter++;
- out.println("Servlet载入,它被访问"+counter+"次,加上初始化的配置,"
- +"这个Servlet共访问了"+(org+counter)+"次");
- }
- //初始化方法,获得初始参数
- publicvoid init() throws ServletException
- {
- String c=getInitParameter("counter");
- try{
- org=Integer.parseInt(c);
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- }
package com.test.ch8
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CounterServlet extends HttpServlet{
int counter=0;
int org=0;
//处理doGet请求
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
counter++;
out.println("Servlet载入,它被访问"+counter+"次,加上初始化的配置,"
+"这个Servlet共访问了"+(org+counter)+"次");
}
//初始化方法,获得初始参数
public void init() throws ServletException
{
String c=getInitParameter("counter");
try{
org=Integer.parseInt(c);
}
catch(Exception e){
e.printStackTrace();
}
}
}
Servlet的名字、类和其他杂项
在配置Servlet时,首先必须指定Servlet的名字、Servlet的类(如果是JSP,必须指定JSP文件的位置)。另外,可以选择性地给Servlet增加一定的描述,并且指定它在部署时显示的名字和部署时显示的Icon。
CounterServlet是这样被配置的如下
- <servlet>
- <description>Servlet配置</description>
- <display-name>TestServletConfig</display-name>
- <servlet-name>CounterServlet</servlet-name>
- <servlet-class>com.test.ch8.CounterServlet</servlet-class>
- .........
- </servlet>
<servlet> <description>Servlet配置</description> <display-name>TestServletConfig</display-name> <servlet-name>CounterServlet</servlet-name> <servlet-class>com.test.ch8.CounterServlet</servlet-class> ......... </servlet>
如果要配置的Servlet是一个JSP文件,那么可以这样设置
- <servlet>
- <servlet-name>NewUserServlet</servlet-name>
- <jsp-file>newUser.jsp</jsp-file>
- </servlet>
<servlet> <servlet-name>NewUserServlet</servlet-name> <jsp-file>newUser.jsp</jsp-file> </servlet>
初始化参数
- <servlet>
- <init-param>
- <param-name>counter</param-name>
- <param-value>10000</param-value>
- </init-param>
- </servlet>
<servlet> <init-param> <param-name>counter</param-name> <param-value>10000</param-value> </init-param> </servlet>
在这个配置中,指定counter的参数值为10000
启动装入优先级 启动装入优先级通过<load-on-startup>配置,例如
- <servlet>
- <servlet-name>NewUserServlet</servlet-name>
- <jsp-file>newUser.jsp</jsp-file>
- <load-on-startup>10</load-on-starup>
- </servlet>
- <servlet>
- <servlet-name>HelloWorldServlet</servlet-name>
- <servlet-class>com.test.HelloWorldServlet</servlet-class>
- <load-on-startup>30</load-on-starup>
- </servlet>
- <servlet>
- <servlet-name>PrintServlet</servlet-name>
- <servlet-class>com.test.HelloWorldServlet2</servlet-class>
- <load-on-startup>AnyTime</load-on-starup>
- </servlet>
<servlet> <servlet-name>NewUserServlet</servlet-name> <jsp-file>newUser.jsp</jsp-file> <load-on-startup>10</load-on-starup> </servlet> <servlet> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class>com.test.HelloWorldServlet</servlet-class> <load-on-startup>30</load-on-starup> </servlet> <servlet> <servlet-name>PrintServlet</servlet-name> <servlet-class>com.test.HelloWorldServlet2</servlet-class> <load-on-startup>AnyTime</load-on-starup> </servlet>
那么可以保证NewUseServlet在HelloWorldServlet之前被载入,PrintServlet可以在服务启动后的任何时候载入。
Servlet的映射 可以给一个Servlet做多个映射,这样,我们可以通过不同的方式访问这个Servlet。例如
- <servlet-mapping>
- <servlet-name>CounterServlet</servlet-name>
- <url-pattern>/count_servlet</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>CounterServlet</servlet-name>
- <url-pattern>/count/*</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>CounterServlet</servlet-name>
- <url-pattern>/test/counterservlet/count.html</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>CounterServlet</servlet-name>
- <url-pattern>/test/count.html</url-pattern>
- </servlet-mapping>
<servlet-mapping> <servlet-name>CounterServlet</servlet-name> <url-pattern>/count_servlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>CounterServlet</servlet-name> <url-pattern>/count/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>CounterServlet</servlet-name> <url-pattern>/test/counterservlet/count.html</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>CounterServlet</servlet-name> <url-pattern>/test/count.html</url-pattern> </servlet-mapping>
通过这些配置,我们可以使用不同的方式访问这个Servlet。有趣的是,对于第二种映射方式,可以通过/count开头,都能访问这个Servlet。
运行安全设置 关于Web应用的安全配置,这里先给出一个简单的例子:CounterServlet的安全配置
- <servlet>
- <run-as>
- <role-name>admin</role-name>
- </run-as>
- <security-role-ref>
- <role-name>admin</role-name>
- <role-link>admin</role-link>
- </security-role-ref>
- </servlet>
<servlet> <run-as> <role-name>admin</role-name> </run-as> <security-role-ref> <role-name>admin</role-name> <role-link>admin</role-link> </security-role-ref> </servlet>