# 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 {

case "client1":
response.sendRedirect("app1/apk-1-index.html");
}
break;

case "client2":
response.sendRedirect("app2/apk-2-index.html");
}
break;

case "client3":
response.sendRedirect("app3/apk-3-index.html");
}
break;

case "client4":
response.sendRedirect("app4/apk-4-index.html");
}
break;
case "client5":
response.sendRedirect("app5/apk-5-index.html");
}
break;
case "client6":
response.sendRedirect("app5/apk-5-index.html");
}
break;
default:
response.sendRedirect("/Test2/index.jsp");
}
}

• Are these two switch statements related in some way? If not I would recommend posting two separate questions. I'm also wondering if you have changed the code in some way before posting it here. What is real and what did you "obfuscate"? – Simon Forsberg May 10 '17 at 20:28
• Thanks for your answer. I will follow your suggestion and separete this post into two questions because these switch statements are unrelated. I don't understand what do you mean telling if I had changed the code - I am the owner of the code. If you are worried or puzzled about security reasons then don't be because it's not public project. – miki May 10 '17 at 20:38
• My concern is if the code you have posted here is your real, actual code. Does the code you have actually say response.sendRedirect("app1/apk-1-index.html"); and response.sendRedirect("app2/apk-2-index.html"); and so on? And does it really say client1, client2, etc or did you examplify it? Please make it clear. – Simon Forsberg May 10 '17 at 20:40
• Note that while some things can be examplified, if you examplify too much that might leave us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR? (Note that at the moment I don't consider this off-topic for this reason) – Simon Forsberg May 10 '17 at 20:41
• Yes it's the same. – miki May 10 '17 at 20:42

### What is common?

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

case <THIS PART IS DIFFERENT>:
response.sendRedirect(<THIS PART IS DIFFERENT>);
}


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<String, String> redirects = new HashMap<>();
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)) {