实验八:异步servlet

注意一:

@WebServlet(urlPatterns={"/hi"},asyncSupported=true)

注意二:

在客户端看来, 这仍然是同步的请求, 客户端需要等待请求结果。

注意三:异步分为请求异步和相应异步, 本例子只是响应方法异步。

 

/**

 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.

 *

 * You may not modify, use, reproduce, or distribute this software except in

 * compliance with  the terms of the License at:

 * http://java.net/projects/javaeetutorial/pages/BerkeleyLicense

 */

package javaeetutorial.hello2;

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.AsyncContext;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

/**

 * This is a simple example of an HTTP Servlet. It responds to the GET method of

 * the HTTP protocol.

 */

@WebServlet(urlPatterns={"/hi"},asyncSupported=true)

public class GreetingServlet extends HttpServlet {

 

@Override

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

 

response.setContentType("text/html");

final AsyncContext context= request.startAsync();

context.start(new Runnable(){

 

@Override

public void run() {

try {

Thread.sleep(10000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// then write the data of the response

try {

context.getResponse().getWriter().println("hello");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

context.complete();

}

 

});

 

 

}

 

 

 

@Override

public String getServletInfo() {

return "The Hello servlet says hello.";

 

}

}

 

你可能感兴趣的:(实验八:异步servlet)