patternjavaMinor
Authenticating and redirecting six specific users
Viewed 0 times
sixandredirectingspecificauthenticatingusers
Problem
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");
}
}Solution
What is common?
Let's take a look at the cases that look similar and see what is common among them.
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!
(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
So, if it is in the map, then let's do the common things and use the redirect we picked from the map:
Let's take a look at the cases that look similar and see what is common among them.
case :
if(PASSWORD.equals(password)) {
request.getSession(true).setAttribute("username", username);
response.sendRedirect();
}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 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)) {
request.getSession(true).setAttribute("username", username);
response.sendRedirect(redirect);
}Code Snippets
case <THIS PART IS DIFFERENT>:
if(PASSWORD.equals(password)) {
request.getSession(true).setAttribute("username", username);
response.sendRedirect(<THIS PART IS DIFFERENT>);
}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");
....String redirect = redirects.get(username);
if (redirect == null) {
response.sendRedirect("/Test2/index.jsp");
return;
}if (PASSWORD.equals(password)) {
request.getSession(true).setAttribute("username", username);
response.sendRedirect(redirect);
}Context
StackExchange Code Review Q#163025, answer score: 2
Revisions (0)
No revisions yet.