分享

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

 昵称70680357 2020-07-02

一.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()
     */
语言 方法
5961 3ZaIv
z3qrs
  • 「免费教你」自媒体怎么赚钱「日赚千元」
  • 7423 2010-05-19 06:38:32
    //作用与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();
    复制代码

      本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
      转藏 分享 献花(0

      0条评论

      发表

      请遵守用户 评论公约

      类似文章 更多