Well we can use some external circuitry to cut off power to the Servo but it’s definitely not needed.
The software will be able to handle it.
I’m thinking the part below might be a problem but just try the code below 1st
servo1.write(22);
delay(1000);
Try:
int testFunction(String args) {
servo1.attach(A0); // attaches the servo on the A0 pin to the servo object
state = !state; // toggle the state
servo1.write(22);
delay(1000);
servo1.detach(); // detach the servo on the A0 pin to the servo object
return 200; // This is checked in the web app controller for validation
}