Skip to content

Instantly share code, notes, and snippets.

@coa00
Last active January 22, 2016 06:36
Show Gist options
  • Save coa00/ad914299fa34db8cfb97 to your computer and use it in GitHub Desktop.
Save coa00/ad914299fa34db8cfb97 to your computer and use it in GitHub Desktop.
Java Servletでリクエストパラメータの存在確認 ref: http://qiita.com/coa00@github/items/1e33fdaa5fbb2d337878
String [] keys = {"u","email"};
String ret = checkKeys(request,keys);
/**
* キー一覧を渡して存在しないキーがあれば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;
}
/**
* 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