Servlet8
一、Annotation 进行配置不需要 web.xml
新建new Servlet 时,不需要在web.xml 文件中生成Servlet的相关信息
1 import java.io.IOException; 2 import java.io.PrintWriter; 3 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 @WebServlet(name="MyServlet",urlPatterns={"/MyServlet"}) 10 public class MyServlet extends HttpServlet { 11 12 public void doGet(HttpServletRequest request, HttpServletResponse response) 13 throws ServletException, IOException { 14 doPost(request, response); 15 } 16 17 public void doPost(HttpServletRequest request, HttpServletResponse response) 18 throws ServletException, IOException { 19 PrintWriter out=response.getWriter(); 20 out.print("hello Filter"); 21 } 22 23 }
1.Servlet
只需要在java源文件的Servlet类前面加上:
@WebServlet(name="",urlPatterns={""},initParams= {@WebInitParam(name="",value=""),loadOnStartup=1})
public class MyServlet extends HttpServlet{}
2.Filter
只需要在java源文件的Filter类前面加上@WebFilter(filterName="MyFilter",urlPatterns="/MyFilter",servletNames={"MyServlet"})
现在只需要在java源文件的Listener类前面加上
@WebListener即可;
二、模块化编程
三、Servlet异步处理
Servlet作为MVC中的控制器,控制器负责分发任务给MODEL完成,然后把结果交给JSP页面显示而如果有许多MODEL ,其中一个MODEL处理时间很长则会导致整个页面显示很慢。
异步处理关键点: 将复杂业务处理另外开一个线程,而Servlet将执行好的业务先送往jsp输出,等耗时业务做完后再送往jsp页面。
先显示一部分,再显示一部分。
异步处理需要在Annotation中注明asyncSupported=true;
1 package test; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.AsyncContext; 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 @WebServlet(name="MyServlet",urlPatterns={"/MyServlet"},asyncSupported = true) 13 public class MyServlet extends HttpServlet { 14 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 doPost(request, response); 18 } 19 public void doPost(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21 PrintWriter out=response.getWriter(); 22 out.print("start..."); 23 System.out.println("start"); 24 //比较耗时的业务 25 //当前请求延迟 26 AsyncContext ac = request.startAsync(); 27 //设置超时时间 28 ac.setTimeout(5*3000); 29 //启动线程 30 ac.start(new MyThread(ac)); 31 out.write("<br>end"); 32 System.out.println("end"); 33 out.flush(); 34 } 35 36 } 37 class MyThread implements Runnable{ 38 private AsyncContext ac = null; 39 public MyThread(AsyncContext ac) { 40 super(); 41 this.ac = ac; 42 } 43 @Override 44 public void run() { 45 try { 46 Thread.sleep(3000); 47 PrintWriter out = ac.getResponse().getWriter(); 48 out.write("<br> 耗时操作完成"); 49 System.out.println("耗时操作完成"); 50 } catch (InterruptedException | IOException e) { 51 e.printStackTrace(); 52 } 53 54 } 55 56 57 }
四、异步Listener
异步监听器用来监听异步处理事件,只需要实现AsyncListener接口即可。
此接口有四个方法:
1 public void onStartAsync(AsyncEvent event)throws IOException; 2 public void onComplete(AsyncEvent event); 3 public void onTimeout(AsyncEvent event); 4 public void onError(AsyncEvent event);
五、文件上传API简化
原本文件上传时通过common-fileeuupload或者SmartUpload,上传比较麻烦,在servlet3.0中不需要导入第三方jar包,并且提供了很方便的文件上传功能。
注意点:
- html中<input type="file">表示文件上传控件;
- form的enctype = "multipart/form-data";
- 在servlet类前加上@MultipartConfig
- request.getPart()获得