Two pages using the same Servlet

Asked

Viewed 561 times

3

I have my main page(index.jsp) uses Servlet for a few little things. Then redirect to the final page.jsp. This page will use Servlet as well, as I need the user to choose the functions (buttons) he wants to use. My question is:
I can use the same Servlet or I have to create one for each button?

1 answer

4


It is possible to use the same Servlet for multiple actions.

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

The biggest problem of this approach is to end up with a spaghetti code, that is, several ifs treating different things in the same method. In short, look at the organization of the code, rather than the concern of having more classes and methods.

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

  • 1

    and how do I check the request parameters??

  • 1

    String str = request.getParameter("foo"); - just make the comparison with the string.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.