Last active
January 22, 2016 06:36
-
-
Save coa00/ad914299fa34db8cfb97 to your computer and use it in GitHub Desktop.
Java Servletでリクエストパラメータの存在確認 ref: http://qiita.com/coa00@github/items/1e33fdaa5fbb2d337878
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String [] keys = {"u","email"}; | |
String ret = checkKeys(request,keys); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* キー一覧を渡して存在しないキーがあればListにいれてかえす。 | |
* @param req | |
* @param keys | |
* @return | |
*/ | |
public static List<String> getNotExistKeys(HttpServletRequest req,String keys[]){ | |
List<String> notExistKeys = new ArrayList<String>(); | |
boolean notExit; | |
for(int i=0;i<keys.length;i++){ | |
notExit = true; | |
Enumeration<String> parameterNames = req.getParameterNames(); | |
while (parameterNames.hasMoreElements()) { | |
String paramName = parameterNames.nextElement(); | |
if (keys[i].equals(paramName)){ | |
String paramValues = req.getParameter(paramName); | |
if (!paramValues.isEmpty()) notExit = false; | |
} | |
} | |
if (notExit == true) notExistKeys.add(keys[i]); | |
} | |
return notExistKeys; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* checkKeys | |
* @param req | |
* @param keys | |
* @return | |
*/ | |
public static String checkKeys(HttpServletRequest req,String keys[]){ | |
List<String> emptyParams = getNotExistKeys(req,keys); | |
String callback = req.getParameter("callback"); | |
if (emptyParams.size()>0){ | |
return emptyParams.toString()+"が存在しません。"; | |
}else{ | |
return "OK"; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment