目录结构
package com.aaa.servlet; 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; /** * Created by 张晨光 on 2020/3/20 16:01 *注解,来实现get请求 post请求的输出... */ @WebServlet("/login") public class HelloServet extends HttpServlet { //上次课的时候,重写的是service方法 //重写doGet()/doPost()方法 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // super.doGet(req, resp); //哪个地方传过来的,是地址栏url传过来的,get请求... System.out.println("get请求"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // super.doPost(req, resp); System.out.println("post请求..."); } }
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 = 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(); } } } 支持多线程
注解源码:
@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 代码
@WebServlet注解用于标注在一个继承了HttpServlet类之上,属于类级别的注解。
用法形如:
@WebServlet("/DisplayHeader1")
// 扩展 HttpServlet 类
public class DisplayHeader extends HttpServlet {
// 处理 GET 方法请求的方法
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
其中 /DisplayHeader1 表示访问该servlet的 url 映射(地址)(此处为相对路径,即 “项目名称/DisplayHeader1” )。
该注解的作用等价于 在web.xml中配置的该servlet的<servlet-mapping>元素中<url-pattern>的配置,比如:
<servlet>
<!-- 类名 -->
<servlet-name>DisplayHeader</servlet-name>
<!-- 所在的包 -->
<servlet-class>test.DisplayHeader</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayHeader</servlet-name>
<!-- 访问的网址 -->
<url-pattern>/DisplayHeader2</url-pattern>
</servlet-mapping>
此时,访问如下两个路径的效果是一样的,不互斥:
http://localhost:8080/test/DisplayHeader1
和
http://localhost:8080/test/DisplayHeader2