2013-04-17 82 views
3

當天的問候!!!將一組單選按鈕的值傳遞給servlet

我創建了一個JSP文件如下:

<%@ 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"> 
<% 
    for(int i=0; i<5; i++){ 
     %> 
      <input type="radio" name="<%= i%>" value="<%=i %>"> 
     <% 
    } 
%> 
<input type="submit" value="SUBMIT"> 
</form> 
</body> 
</html> 

variable.Now我想要得到的單選按鈕的值,這個程序創建一個循環有名稱和值相同環路5個單選按鈕在servlet中。對於我的servlet的代碼如下:

import java.io.IOException; 
import java.io.PrintWriter; 

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(); 
    for (int i=0; i<5; i++){ 
    String s[] = new String[5]; 
    s[i] = request.getParameter("<%= i%>"); 
    out.println(s[i]); 
    } 
} 
} 

但我只用5「空」值結束。我知道問題在哪裏。但我無法找到一種方法將循環變量傳遞給request.getParameter()方法。請幫助我..... Thnx提前!

回答

1

在java中,你應該寫:

s[i] = request.getParameter(Integer.toString(i)); 

<%= ...%>的JSP腳本,而不是Java類。

相關問題