@WebServlet注解下 各属性说明

一、@WebServlet 注解属性说明

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WebServlet {
    String name() default "";

    String[] value() default {};

    String[] urlPatterns() default {};

    int loadOnStartup() default -1;

    WebInitParam[] initParams() default {};

    boolean asyncSupported() default false;

    String smallIcon() default "";

    String largeIcon() default "";

    String description() default "";

    String displayName() default "";
}
WebServlet 代码

1.属性列表:

属性名 类型 描述
name String servlet-name,如果没有显示指定,该Servlet的取值为全限定名
value String[] 等价于 urlPatterns 属性,与该属性不能同时使用
urlPatterns String[] 指定Servlet url的匹配模式,等价于
loadOnStartup int 指定Servlet的加载顺序
initParams webInitParam[] 指定初始化参数
asyncSupported boolean 是否支持异步操作
description String 描述
displayName String servlet显示名
     

 

 

 

 

 

 

 

 

 

二、属性说明

1.initParams:

      initParams={@WebInitParam(paramName="zsms",paramValue="醉生梦死")//在服务器初始化的时候会打印,因为调用Init方法}

package com.kenny.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * asyncSupported = true 是否支持异步请求
 */
@WebServlet(value = "/hello",asyncSupported = true,initParams = {
                                                                    @WebInitParam(name="name",value = "Kenny"),
                                                                    @WebInitParam(name="age",value = "20")
                                                                }
           )
public class HelloServlet extends HttpServlet {

    String name;
    String age;

    @Override
    public void init(ServletConfig config) throws ServletException {
        name = config.getInitParameter("name");
        age = config.getInitParameter("age");

        System.out.println("init--------- name: "+ name +" ,---------- age: "+age);

        super.init(config);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet--------- name: "+ name +" ,---------- age: "+age);

        resp.getWriter().write("hello,thank you "+ name+" ,and you age is "+ age);
    }
}
servlet initParams 事例

 

2.urlPatterns/value(不可以同时使用):

      Servlet 2.5开始,一个servlet可以使用多个url-pattern规则,因此 urlPatterns 属性也支持数组配置;web.xml 配置下,是采用 servlet-mapping 和当servlet容器接收到浏览器发起的一个url请求后,容器会用url减去当前应用的上下文路径,以剩余的字符串作为servlet映射,假如url是http://localhost:8080/ServletTest/index.do,其应用上下文(context)是ServletTest。http://localhost:8080/ServletTest去掉,用剩下的/index.do部分拿来做servlet的映射匹配;url-pattern映射匹配过程是有优先顺序的(精确匹配>路径匹配>扩展名匹配>缺省匹配)而且当有一个servlet匹配成功以后,就不会去访问剩下的servlet了。

     可以使用数组方式:urlPatterns={"/servlet/url1","/servlet/url2"}

3.asyncSupported

模拟代码,可以打开多个浏览器查看

//Servlet
package com.kenny.servlet;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet(urlPatterns = "/async",asyncSupported = true)
public class AsyncServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.println("进入Servlet的时间:" + new Date() + ".");
        out.flush();

        //在子线程中执行业务调用,并由其负责输出响应,主线程退出
        AsyncContext ctx = req.startAsync();
        new Thread(new Executor(ctx)).start();

        out.println("结束Servlet的时间:" + new Date() + ".");
        out.flush();
    }
}

//模拟多线程
package com.kenny.servlet;

import javax.servlet.AsyncContext;
import java.io.PrintWriter;
import java.util.Date;

    public class Executor implements Runnable {
        private AsyncContext ctx = null;
        public Executor(AsyncContext ctx){
            this.ctx = ctx;
        }

        public void run(){
            try {
                //等待十秒钟,以模拟业务方法的执行
                Thread.sleep(10000);
                PrintWriter out = ctx.getResponse().getWriter();
                out.println(Thread.currentThread().getId()+"----业务处理完毕的时间:" + new Date() + ".");
                out.flush();
                ctx.complete();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
支持多线程

 

你可能感兴趣的:(@WebServlet注解下 各属性说明)