1

我创建了一个 JSP 页面,其单选按钮与当月的天数相同。代码是:

<%@page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>
    <form action="TestAms" method="post">
    <%
        Calendar cal = Calendar.getInstance();
        int md = cal.getActualMaximum(Calendar.DATE);
        for(int i=0; i<md; i++){
            %>
                <input type="radio" name="<%= i%>" value="<%=i %>">
            <%
        }
    %>
    <input type="submit" value="SUBMIT">
    </form>
</body>
</html>

现在我在 servlet 中获得单选按钮(JSP 页面)的响应并将它们存储在哈希表中,然后打印哈希表的元素,但我没有得到预期的结果。如果没有 HashTable,它可以正常工作,但我希望它可以与 HashTable 一起使用。Servlet 代码为:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestAms extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    Calendar cal = Calendar.getInstance();
    int md = cal.getActualMaximum(Calendar.DATE);

    Hashtable ht = new Hashtable<>();
    Enumeration sh = ht.keys();

    for (int y=0; y<md; y++){
        String s[] = new String[md+1];
        s[md] = request.getParameter(Integer.toString(y)); 
        if(s[md]!=null){
            //out.println(s[md]);
            ht.put(y, s[md]);
        }
    }

    while(sh.hasMoreElements()){
        System.out.println(sh.nextElement() );
    }
}
}
4

1 回答 1

1

我在 servlet 中获得单选按钮(JSP 页面)的响应并将它们存储在哈希表中,然后打印哈希表的元素,

请在下面找到将请求参数值存储在Hashtable. 我希望这有效:

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        Hashtable<String, String> days = new Hashtable<>();
        Enumeration<String> names = request.getParameterNames();
        while(names.hasMoreElements()){
            String param = names.nextElement();
            String value = request.getParameter(param);
            days.put(param, value);
        }
        System.out.println(days);
}
于 2013-04-21T05:38:39.950 回答