@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的匹配模式,等价于<url-parttern>
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 = InitParameter("name");
age = InitParameter("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);
}
}
servlet initParams 事例
2.urlPatterns/value(不可以同时使⽤):
可以使⽤数组⽅式:urlPatterns={"/servlet/url1","/servlet/url2"}
3.asyncSupportedparam name
模拟代码,可以打开多个浏览器查看
//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 = Writer();
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){
< = ctx;
}
public void run(){
try {
//等待⼗秒钟,以模拟业务⽅法的执⾏
Thread.sleep(10000);
PrintWriter out = Response().getWriter();
out.println(Thread.currentThread().getId()+"----业务处理完毕的时间:" + new Date() + "."); out.flush();
ctxplete();
} catch (Exception e) {
e.printStackTrace();
}
}
}
⽀持多线程
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论