Most vaccines work by teaching the immune system to recognize harmful viruses or bacteria before a person gets infected and becomes unwell. A few cancer vaccines do this, too. For example, a vaccine is given to young people as protection against human papillomaviruses that can cause cervical cancer.