This is a web project
The first is Web. XML (note the version number and schema file in XML)
<? XML version = "1.0" encoding = "UTF-8"?> <Web-app version = "3.0" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <! -- Servlet3.0 requires tomcat7 and javaee6. For the configuration method, see --> <! -- Http://blog.csdn.net/jadyer/article/details/9164655 --> <! -- Http://blog.csdn.net/jadyer/article/details/9164737 --> </Web-app>
Below is a common servlet implemented using the servlet3.0 Annotation
Package COM. jadyer. servlet; import Java. io. ioexception; import Java. io. printwriter; 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;/*** new servlet annotation of servlet3.0 * @ see 1) modify web. XML, change version to 3.0, change schema to web-app_3_0.xsd * @ see 2) in the servlet, use the annotation * @ create Jun 23,201 3 5:46:18 * @ author Xuan Yu Package COM. jadyer. servlet; import Java. io. ioexception; import Java. io. printwriter; import Java. util. date; import javax. servlet. asynccontext; import javax. servlet. asyncevent; import javax. servlet. asynclistener; 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 COM. jadyer. thread. helloasyncthread;/*** asynchronous support for new features of servlet3.0 * @ see variables * @ see asynchronous implementation overview * @ see 1) servlet2.5 can also start a separate thread to execute time-consuming tasks, then the servlet will continue to execute * @ see. When the last line of code is executed, the servlet will output the response to the requester, and any execution results of that thread will not be reflected to the requester * @ see 2) asynchronous support in servlet3.0 is also a separate start thread to execute time-consuming tasks, when the servlet executes the last line of code * @ see servlet3.0 provides some mechanisms to determine whether the thread has completed execution, it will not respond to all results until the thread completes execution to the requester * @ see (so when the browser accesses the servlet in this example, it will first rotate to load ..., the browser displays the five rows of time after the asynchronous thread execution is completed in five seconds.) * @ see 3) if the thread is not started separately to execute time-consuming tasks, it is not desirable to put the time-consuming operation serial into the servlet Method for execution * @ see because the servlet container manages a thread pool, the threads attached to the servlet processing our request come from this thread pool * @ see. If an operation is too time-consuming, the resources in the thread pool will be occupied, therefore, servlet3.0 has asynchronous support * @ see slave * @ see asynchronous implementation step * @ see 1) enable asynchronous support in servlet (asynchronous support is not supported by default ), @ webservlet (asyncsupported = true) * @ see 2) Enable an asynchronous context object in the servlet, asynccontext context = request. startasync (); * @ see 3) create a separate thread to execute time-consuming operations. After the time-consuming operations are completed, call asynccontext. complete (); * @ see 4) start the time-consuming operation thread in servlet * @ see references * @ create Jun 23,201 3 5:47:55 * @ author Xuan Yu
Finally, thread classes used for asynchronous support
Package COM. jadyer. thread; import Java. io. printwriter; import Java. util. date; import javax. servlet. asynccontext; /*** servlet3.0 new feature asynchronous support used Thread class * @ create Jun 23,201 3 6:05:55 * @ author Xuan Yu