我需要获取我的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
了最后一个元素。MyHashTable
包含键 {"Section"、"Name"、"Standard"} 并且在 while 循环中 'e' 仅获得值 "Section" 和 "Standard"。第三次抛出异常。
我也试过,[链接](从 Java 中的 HashMap 获取密钥)但同样的问题发生在这里。