如何编写一个Servlet处理类并设置其初始化参数

一.Servlet继承体系

Servlet接口:总接口,定义了Servlet的不同生命周期行为

ServletConfig接口:得到对应Servlet的各种参数

GennericServlet类:是一个抽象类,实现了大部分需要的Servlet方法,大多数Servlet行为都在这里实现

HttpServlet类:抽象类,基于HTTP请求的特性,继承自GennericServlet,实现了自己的service()方法

MyTestServlet:自定义处理类,实现具体的doGet().doPost()方法

//使用注解设置初始化参数
@WebServlet(value = "/test",loadOnStartup = 0)
public class MyTestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
    }
}

 二.初始化Servlet参数

使用注解初始化@WebServlet

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WebServlet {

    /**
     * @return name of the Servlet
     */
    //指定Servlet名字,默认为包名加上类名
    String name() default "";

    /**
     * A convenience method, to allow extremely simple annotation of a class.
     *
     * @return array of URL patterns
     * @see #urlPatterns()
     */
    //作用与urlPatterns相同,指定Servlet处理的URL但不能与urlPatterns同时出现
    String[] value() default {};

    /**
     * @return array of URL patterns to which this Filter applies
     */
    //指定Servlet处理的URL
    String[] urlPatterns() default {};

    /**
     * @return load on startup ordering hint
     */
    //指定Servlet的加载顺序
    int loadOnStartup() default -1;

    /**
     * @return array of initialization params for this Servlet
     */
    //指定初始化参数类型是@webinitparam注解数组
    WebInitParam[] initParams() default {};

    /**
     * @return asynchronous operation supported by this Servlet
     */
    boolean asyncSupported() default false;

    /**
     * @return small icon for this Servlet, if present
     */
    String smallIcon() default "";

    /**
     * @return large icon for this Servlet, if present
     */
    String largeIcon() default "";

    /**
     * @return description of this Servlet, if present
     */
    String description() default "";

    /**
     * @return display name of this Servlet, if present
     */
    String displayName() default "";
}
    

三.获取初始化参数

调用ServletConfig接口中的getInitParameter(String name)方法获取指定的参数值,返回String

  /**
     * Returns a <code>String</code> containing the value of the named
     * initialization parameter, or <code>null</code> if the parameter does not
     * exist.
     *
     * @param name
     *            a <code>String</code> specifying the name of the
     *            initialization parameter
     * @return a <code>String</code> containing the value of the initialization
     *         parameter
     */
    public String getInitParameter(String name);

调用ServletConfig接口中的getInitParameterNames()方法获取全部参数,返回Enumeration<String>

   /**
     * Returns the names of the servlet's initialization parameters as an
     * <code>Enumeration</code> of <code>String</code> objects, or an empty
     * <code>Enumeration</code> if the servlet has no initialization parameters.
     *
     * @return an <code>Enumeration</code> of <code>String</code> objects
     *         containing the names of the servlet's initialization parameters
     */
    public Enumeration<String> getInitParameterNames();

 

posted @ 2020-07-01 22:46  木子李和三点水  阅读(663)  评论(0编辑  收藏  举报