Servlet3 has been out for several years, and how many people know about its new features? Here is a brief introduction.

The following features are added:

1, asynchronous processing support

2. Pluggability support

3, annotation support, zero configuration, no need to configure web. XML

.

What the hell is asynchronous processing?

Straight up the keyboard dry.

@WebServlet(name = “index”, urlPatterns = { “/” }, asyncSupported = true)

public class IndexServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); try { PrintWriter out = resp.getWriter(); out.println("servlet started.<br/>"); out.flush(); AsyncContext asyncContext = req.startAsync(); asyncContext.addListener(getListener()); asyncContext.start(new IndexThread(asyncContext)); out.println("servlet end.<br/>"); out.flush(); } catch (Exception e) { e.printStackTrace(); Private AsyncListener getListener() {return new AsyncListener() {return new AsyncListener() { public void onComplete(AsyncEvent asyncEvent) throws IOException { asyncEvent.getSuppliedResponse().getWriter().close();  System.out.println("thread completed."); } public void onError(AsyncEvent asyncEvent) throws IOException { System.out.println("thread error."); } public void onStartAsync(AsyncEvent asyncEvent) throws IOException { System.out.println("thread started."); } public void onTimeout(AsyncEvent asyncEvent) throws IOException { System.out.println("thread timeout."); }}; } } public class IndexThread implements Runnable { private AsyncContext asyncContext; public IndexThread(AsyncContext asyncContext) { this.asyncContext = asyncContext; } public void run() { try { Thread.sleep(5000); PrintWriter out = asyncContext.getResponse().getWriter(); out.println("hello servlet3.<br/>"); out.flush(); asyncContext.complete(); } catch (Exception e) { e.printStackTrace(); }}}Copy the code

Access localhost: 8080 / test

The page is printed first

servlet started.
servlet end.Copy the code

Output again after 5 seconds

hello servlet3.Copy the code

It can be seen that the servlet immediately returned, but did not close the response flow, but just passed the response response to the thread, the thread continues to output, we can compare the resource consumption of the time to do asynchronously, which greatly saves the servlet resources.

Servlet3 asynchronous processing has also been added to Springmvc3.2 for those who are interested.

As you can see from the servlet annotations above, Servlet3 completely frees up the web.xml configuration by making annotations that completely replace the web.xml configuration.

Read more on my blog:

1.Java JVM, Collections, Multithreading, new features series tutorials

2.Spring MVC, Spring Boot, Spring Cloud series tutorials

3.Maven, Git, Eclipse, Intellij IDEA series tools tutorial

4.Java, backend, architecture, Alibaba and other big factory latest interview questions

Life is good. See you tomorrow