Two pages using the same Servlet

3

I have my main page (index.jsp) use the servlet for some small things. Then redirect to the final page.jsp. This page will use the servlet as well, since I need the user to choose the functions (buttons) he wants to use. My question is:
Can I use the same servlet or do I have to create one for each button?

    
asked by anonymous 03.11.2014 / 22:17

1 answer

4

You can use the same Servlet for several actions.

A simple way is to put a name attribute on each button and then check inside Servlet, in the request parameters, which button was pressed. There will be an attribute with the same name of the button pressed.

The biggest problem with this approach is to end up with a spaghetti code, that is, several ifs dealing with different things in the same method. In short, watch out for the organization of the code, rather than worrying about having more classes and methods.

If it's not a trivial project, consider a framework like Spring MVC to make your work easier.

    
03.11.2014 / 22:36