gpt4 book ai didi

java - 将动态数据传递到 servlet 中的哈希表

转载 作者:太空宇宙 更新时间:2023-11-04 07:40:31 25 4
gpt4 key购买 nike

我创建了一个 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 页面)的响应,并将它们存储在哈希表中,然后打印哈希表的元素,但我没有得到预期的结果。如果没有哈希表,它可以正常工作,但我希望它可以与哈希表一起使用。 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() );
}
}
}

最佳答案

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

请在下面找到优化版本,它将请求参数值存储在哈希表中。我希望这能起作用:

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);
}

关于java - 将动态数据传递到 servlet 中的哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16127512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com