Here is my Java class controller:
public class Controller extends HttpServlet {
private Chooser chooser = Chooser.INSTANCE;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp);
}
private void processRequest(HttpServletRequest req, HttpServletResponse resp) {
try {
String page = chooser.chooseCommand(req.getParameter("command")).execute(req, resp);
req.getRequestDispatcher(page).forward(req, resp);
} catch (ServletException | IOException e) {
e.printStackTrace();
}
}
}
Now, let's look at the ENUM class that selects a page:
public enum Chooser {
INSTANCE;
private Map<String, ICommand> commandMap = new HashMap<>();
private Chooser() {
// commands for profession
commandMap.put("professions", new ProfessionCommand());
commandMap.put("addProfession", new AddProfessionCommand());
commandMap.put("saveProfession", new SaveProfessionCommand());
commandMap.put("deleteProfession", new DeleteProfessionCommand());
commandMap.put("editProfession", new EditProfessionCommand());
public ICommand chooseCommand(String command) {
return commandMap.get(command);
}
}
There's also an interface, ICommand:
public interface ICommand {
String execute(HttpServletRequest request, HttpServletResponse resp);
}
Now, let's check out the DeleteProfessionCommand class:
public class DeleteProfessionCommand implements ICommand {
private ApplicantDBProvider provider = ApplicantDBProvider.INSTANCE;
@Override
public String execute(HttpServletRequest request, HttpServletResponse resp) {
try {
Long professionId = Long.parseLong(request.getParameter("id"));
provider.deleteProfession(professionId);
} catch (Exception e) {
request.setAttribute("error", e);
return "pages/error.jsp";
}
return "controller?command=professions";
}
}
In my JSP file, I have an anchor tag like this to delete a row:
<a href="controller?command=deleteProfession&id=${profession.getId()}">Delete</a>
My question now is, how can I display an alert message when I click on delete with options to confirm or cancel. I am still learning Java and would appreciate any help. Thank you!