我需要获取HashTable
中存在的所有密钥。我使用以下代码执行此操作:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Important: doGet get the 4 input.
// Parameter name: "Standard", "Section", "Name", "Rollno"
// But i am dealing with only 3 input to test the code. (not considering "Rollno" as of now, So RollNo will be empty string as of now.)
String output = "";
response.setContentType("text/html");
// referer tag in the header tells that which page sent the request.
// If this request is sent by Search.jsp page, the look at which button
// made the request and process the request accordingly.
if (request.getHeader("referer") != null
&& (request.getHeader("referer").endsWith("Search.jsp"))) {
Enumeration<String> parma_names = request.getParameterNames();
while ((parma_names != null) && (parma_names.hasMoreElements())) {
String ele_name = parma_names.nextElement();
if (ele_name.equalsIgnoreCase("data1")) {
output = requestMadeForDialog(request, ele_name);
response.getWriter().write(output);
} else {
// See here...
output = requestMadeForReport(request, ele_name);
response.getWriter().write(output);
break;
}
}
} else {
// something here...
}
}
如果请求是针对报告的,那么下面是报告的代码。
private String requestMadeForReport(HttpServletRequest request,
String ele_name) {
log("requestMadeForReport and element name is " + ele_name);
// received parameter may be empty, If param is empty then it should not
// be included in where clause.
String standard = request.getParameter("standard");
String section = request.getParameter("section");
String name = request.getParameter("name");
String rollno = request.getParameter("rollno");
Hashtable<String, String> param = new Hashtable<>();
if (standard != null && (!standard.isEmpty())) {
param.put("standard", standard);
}
if (section != null && (!section.isEmpty())) {
param.put("section", section);
}
if (name != null && (!name.isEmpty())) {
param.put("name", name);
}
if (rollno != null && (!rollno.isEmpty())) {
param.put("rollno", rollno);
}
generateReport(param);
return null;
}
//只有非空和非空参数才会移交给实际的报告生成器方法。
private void generateReport(Hashtable<String, String> param) {
// TODO Auto-generated method stub
Enumeration<String> keyset = param.keys();
while (keyset.hasMoreElements()) {
String e = keyset.nextElement();
}
}
但是在得到最后一个元素时我得到NoSuchElementException
。
我的HashTable
包含密钥{“Section”,“Name”,“Standard”}
并且在while循环'e'中只获得值“Section”和“Standard”。
并且第三次抛出异常。
我也尝试过,[link](Get keys from HashMap in Java)但是同样的问题发生在这里。