package com.myeclipseide.asynccontext;

import java.util.Random;

import javax.servlet.AsyncContext;
import javax.servlet.ServletOutputStream;

public class AsyncDispatcher implements Runnable {

	AsyncContext ac;
	ServletOutputStream outputStream;

	public AsyncDispatcher(AsyncContext ac, ServletOutputStream outputStream) {
		this.ac = ac;
		this.outputStream = outputStream;
	}

	public void run() {

		Float value = new Random().nextFloat() * 1000;
		try {

			Thread.sleep(value.longValue());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		ac.getRequest().setAttribute("name",
				ac.getRequest().getParameter("firstname"));
		ac.getRequest().setAttribute("myFavTech",
				ac.getRequest().getParameter("favTech"));
		ac.getRequest().setAttribute("myAppServer",
				ac.getRequest().getParameter("appServer"));
		if (ac.getRequest().getParameter("meUsage") == null) {
			ac.getRequest().setAttribute("myMEUsage", "have not");
		} else if (ac.getRequest().getParameter("meUsage").contentEquals("on")) {
			ac.getRequest().setAttribute("myMEUsage", "have");

		}
		ac.getRequest().setAttribute("myComment",
				ac.getRequest().getParameter("comment"));
		ac.dispatch("/ResultServlet");

	}

}
