	protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        if (session == null) {
        }
        Connection connection;
        try {
			connection = DbAdaptor.connect();
		} 
        catch (Exception e1){
        	out.println("db connection failed");
			e1.printStackTrace();
			return;
		}
		List<FileItem> items = null;
    	FileItem files = null;
    	String taskDir = "";
    	int taskID = -1;
		try {
			items = (List<FileItem>) new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
		} catch (FileUploadException e) {
			e.printStackTrace();
			return;
		}
    	for( FileItem item : items ) {
        	if(item.isFormField()) {
        		String field = item.getFieldName();
        		if (field.equals("task")) {
            		String task = item.getString();
        			if (task == null) {
        			} else {
            			taskDir = TASKS_DIRECTORY + task + "/";        				
        			}
        		}
        		if( field.equals("taskID") ) {
        			taskID = Integer.parseInt(item.getString());
        		}
        	}
        	else {
        		files = item;
        	}
    	}
    	PreparedStatement checkTask;
		try {
			checkTask = connection.prepareStatement("SELECT id FROM tasks WHERE id = ? AND submitter = ?");
			checkTask.setInt(1, taskID);
			int accountID = 1;
			checkTask.setInt(2, accountID);
	    	if (!checkTask.execute()) {
	    		out.println("invalid Task");
	    		response.sendRedirect("/upload.html");
	    	}
		} catch (SQLException e) {
			out.println("SQL problem slecting task from id"); 
			e.printStackTrace();
			return;
		}
		List<String> filenames = new LinkedList<String>();
		if (files.getName().substring(files.getName().length()-4).toLowerCase().equals(".zip")) {
			ZipInputStream zipIn = new ZipInputStream(files.getInputStream());
			ZipEntry entry = zipIn.getNextEntry();
			while( entry != null ) {
				String filename = entry.getName();
				OutputStream fileOut = new FileOutputStream(taskDir + filename);
				IOUtils.copy(zipIn, fileOut);
				zipIn.closeEntry();
				fileOut.close();
				filenames.add(filename);
				entry = zipIn.getNextEntry();
			}
			zipIn.close();
		} else {
			String filename = files.getName();
			filenames.add(filename);        
	        InputStream fileIn = files.getInputStream();
	        OutputStream fileOut = new FileOutputStream(taskDir + filename);
	        IOUtils.copy(fileIn, fileOut);
	        fileIn.close();
	        fileOut.close();
		}
    	for (int i = 0 ; i < filenames.size() ; i++) {
	        String insertQuery = "INSERT INTO subtasks VALUES ?,?,?";
	        String filename = filenames.get(i);
	        PreparedStatement stmt;
	        try {
				stmt = connection.prepareStatement(insertQuery);
				stmt.setInt(1, taskID);
		        stmt.setString(2, filename);
		        stmt.setBoolean(3,  true);
		        stmt.execute();
	        } catch (SQLException e1) {
				System.err.println("some error with task fields: taskID not valid?");
				System.err.println("taskID: " + taskID + ", filename: " + filename);
				return;
			}
    	}		
        out.println("<html>");
        out.println("<body>");
        out.println("uploaded to task " + taskDir + "<br>");                	
        out.println("click <a href=../index.jsp>here</a> to return to the homepage");
        out.println("</body>");
        out.println("</html>");
    }
}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        if (session == null) {
        }
        Connection connection;
        try {
			connection = DbAdaptor.connect();
		} 
        catch (Exception e1){
        	out.println("db connection failed");
			e1.printStackTrace();
			return;
		}
		List<FileItem> items = null;
    	FileItem files = null;
    	String taskDir = "";
    	int taskID = -1;
		try {
			items = (List<FileItem>) new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
		} catch (FileUploadException e) {
			e.printStackTrace();
			return;
		}
    	for( FileItem item : items ) {
        	if(item.isFormField()) {
        		String field = item.getFieldName();
        		if (field.equals("task")) {
            		String task = item.getString();
        			if (task == null) {
        			} else {
            			taskDir = TASKS_DIRECTORY + task + "/";        				
        			}
        		}
        		if( field.equals("taskID") ) {
        			taskID = Integer.parseInt(item.getString());
        		}
        	}
        	else {
        		files = item;
        	}
    	}
    	PreparedStatement checkTask;
		try {
			checkTask = connection.prepareStatement("SELECT id FROM tasks WHERE id = ? AND submitter = ?");
			checkTask.setInt(1, taskID);
			int accountID = 1;
			checkTask.setInt(2, accountID);
	    	if (!checkTask.execute()) {
	    		out.println("invalid Task");
	    		response.sendRedirect("/upload.html");
	    	}
		} catch (SQLException e) {
			out.println("SQL problem slecting task from id"); 
			e.printStackTrace();
			return;
		}
		List<String> filenames = new LinkedList<String>();
		if (files.getName().substring(files.getName().length()-4).toLowerCase().equals(".zip")) {
			ZipInputStream zipIn = new ZipInputStream(files.getInputStream());
			ZipEntry entry = zipIn.getNextEntry();
			while( entry != null ) {
				String filename = entry.getName();
				OutputStream fileOut = new FileOutputStream(taskDir + filename);
				IOUtils.copy(zipIn, fileOut);
				zipIn.closeEntry();
				fileOut.close();
				filenames.add(filename);
				entry = zipIn.getNextEntry();
			}
			zipIn.close();
		} else {
			String filename = files.getName();
			filenames.add(filename);        
	        InputStream fileIn = files.getInputStream();
	        OutputStream fileOut = new FileOutputStream(taskDir + filename);
	        IOUtils.copy(fileIn, fileOut);
	        fileIn.close();
	        fileOut.close();
		}
    	for (int i = 0 ; i < filenames.size() ; i++) {
	        String insertQuery = "INSERT INTO subtasks VALUES (DEFAULT,?,?,?)";
	        String filename = filenames.get(i);
	        PreparedStatement stmt;
	        try {
				stmt = connection.prepareStatement(insertQuery);
				stmt.setInt(1, taskID);
		        stmt.setString(2, filename);
		        stmt.setBoolean(3,  true);
		        stmt.execute();
	        } catch (SQLException e1) {
				System.err.println("some error with task fields: taskID not valid?");
				System.err.println("taskID: " + taskID + ", filename: " + filename);
				return;
			}
    	}		
        out.println("<html>");
        out.println("<body>");
        out.println("uploaded to task " + taskDir + "<br>");                	
        out.println("click <a href=../index.jsp>here</a> to return to the homepage");
        out.println("</body>");
        out.println("</html>");
    }
}
