Authenticating and redirecting six specific users In a project I have a few long switch statements which seem ugly to me. Please suggest how to refactor the following code.

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

     switch(username) {
     case "client1":
         if(PASSWORD.equals(password)) {
             request.getSession(true).setAttribute("username", username);
             response.sendRedirect("app1/apk-1-index.html");
         }
         break;

     case "client2":
         if(PASSWORD.equals(password)) {
             request.getSession(true).setAttribute("username", username);
             response.sendRedirect("app2/apk-2-index.html");
         }
         break;

     case "client3":
         if(PASSWORD.equals(password)) {
             request.getSession(true).setAttribute("username", username);
             response.sendRedirect("app3/apk-3-index.html");
         }
         break;

     case "client4":
         if(PASSWORD.equals(password)) {
             request.getSession(true).setAttribute("username", username);
             response.sendRedirect("app4/apk-4-index.html");
         }
         break;
     case "client5":
         if(PASSWORD.equals(password)) {
             request.getSession(true).setAttribute("username", username);
             response.sendRedirect("app5/apk-5-index.html");
         }
         break;
     case "client6":
         if(PASSWORD.equals(password)) {
             request.getSession(true).setAttribute("username", username);
             response.sendRedirect("app5/apk-5-index.html");
         }
         break;
    default:
        response.sendRedirect("/Test2/index.jsp");
     }
}
What is common?

Let's take a look at the cases that look similar and see what is common among them.

case &lt;THIS PART IS DIFFERENT&gt;:
if(PASSWORD.equals(password)) {
    request.getSession(true).setAttribute("username", username);
    response.sendRedirect(&lt;THIS PART IS DIFFERENT&gt;);
}


Okay, so we have essentially that one string (username) leads to another string (a redirect). One thing is pointing at another. Oh, a map/dictionary!

Map&lt;String, String&gt; redirects = new HashMap&lt;&gt;();
redirects.put("client1", "app1/apk-1-index.html");
redirects.put("client2", "app2/apk-2-index.html");
redirects.put("client3", "app3/apk-3-index.html");
....


(Yes, you could use a simple for-loop to setup this)

Then we have to determine first of all: Is our username in the map or not? If it isn't, that corresponds to our default case.

String redirect = redirects.get(username);
if (redirect == null) {
    response.sendRedirect("/Test2/index.jsp");
    return;
}


So, if it is in the map, then let's do the common things and use the redirect we picked from the map:

if (PASSWORD.equals(password)) {
    request.getSession(true).setAttribute("username", username);
    response.sendRedirect(redirect);
}