0

This is the code of my servlet:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        String result = (String)request.getParameter("action");

        switch (result)
        {
            case "init":
                request.setAttribute("CountriesList", signUpBean.getContriesList());
                String arg = "/signup.jsp";
                RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(arg);
                dispatcher.forward(request, response);
                break;
            case "submit":
                //Code to handle the request
                break;
        }
    }

When I click on the link signup?action=init the servlet is initialized receiving the countries list and passing it to the signup.jsp page in order to show the list on a select form element. When I compile the signup form, I push the submit button which has action="signup?action=submit" in the way the servlet can handle the request. Do you know a more elegant way or correct way to do what I mean?

Roman C
  • 47,329
  • 33
  • 60
  • 147
Mazzy
  • 11,255
  • 34
  • 110
  • 179
  • What do you mean by more elegant? Submit a code for your jsp. – Roman C Jun 09 '14 at 08:55
  • I mean is it the right way to handle different requests in the same servlet in that way? Does exist a different way to handle more requests in the same servlet? – Mazzy Jun 09 '14 at 09:00
  • 1
    I'd suggest it's better to use different servlets for different requests as a good practice.. A few reasons being that it's gonna use up the resources anyway and Java handles these requests synchronously (except Java 8). – Sid Jun 09 '14 at 09:05
  • @Mazzy Define _different requests_. At the moment I don't see how they are different. And instead of asking me contrary question answer my comments and do what it's said in them. – Roman C Jun 09 '14 at 09:15
  • @RomanC for different requests I mean I call the Servlet twice. For init porpouse and submit porpouse – Mazzy Jun 09 '14 at 09:30
  • @Mazzy So, what is different in them the parameter or what? – Roman C Jun 09 '14 at 09:33
  • @RomanC. In your opinion how do you would handle the init and submit events? By creating two different Servlet or so what? Servlet has one method called init to initialize the Servlet. It would be great if it could be used but unfortunately it doesn't fit in this situation. – Mazzy Jun 09 '14 at 12:05
  • I don't know how would you like to handle it, IMO I would map em to the different URL. Then parse the URL and get action mapping from it. When mapping is available get the action config and instantiate a method mapped to the action. You should know that such things is only possible if you are using high level frameworks implementing MVC pattern. – Roman C Jun 09 '14 at 13:19
  • Unfortunately this is not my case since I have to work with Java EE and glassfish. No framework – Mazzy Jun 09 '14 at 15:33

2 Answers2

1

If the init case is to be handled before form submission and the submit case after, it's always gonna have two different requests. I guess in that case the best practice is to use two different servlets. Also, I'd suggest that if the country list isn't dynamic, it would be better to fetch it from a static JSON/XML file instead of having to send a servlet request. That's just a suggestion and your case is a bit unclear to judge that.

Sid
  • 383
  • 2
  • 12
1

Create interface Action and several its implementations. In your case Init and Submit. Register these actions in init method of servlet, so that they are stored in Map<String, Action> actions.

Now your processRequest() will look like:

String actionName = (String)request.getParameter("action");
Action action = actions.get(actionName);
action.perform();

However it seems that you try to re-invent the wheel. Use one of available frameworks. For example Struts does approximately what I explained here. Spring is much stronger and has the same concept.

AlexR
  • 109,181
  • 14
  • 116
  • 194
  • Unfortunately I have to use only java. EE with GlassFish without any framework – Mazzy Jun 09 '14 at 09:24
  • However what is stronger or weaker is primarily opinion based, FYI Struts has integration with Spring seamlessly. – Roman C Jun 09 '14 at 09:25