2014-10-27 74 views
0

我有f.jsp返回一个年龄列表(1..100)和性别(m/f)和一个按钮GO并把它放在组合框中,我有f_m。 java是一个servlet,它从组合框中选择了一些项目,然后从数据库中选择一个项目并将它放在f.jsp的表格中。现在我的问题是当试图在f.jsp中打印表格并且出现HTTP 500时,我做的。这里是我的代码 f.jspHTTP 500重定向到相同的jsp

<% List<String> sex = (List<String>)request.getAttribute("sexList"); %> 
<% List<String> age = (List<String>)request.getAttribute("ageList"); %> 
<form method ="GET" action="f_m" > 
    <html> 
    <body> 
     <table> 
     <tr> 
       <td>Gender:</td> 
       <td><select name="sex"> 
        <%for(String item : sexList) { %> 
         <option value="<%=item%>"><%=item %></option><%}%> 
        </select> 
       </td> 

       <td>age:</td> 
       <td><select name="age"> 
        <%for(String item : maritalStatus) { %> 
         <option value="<%=item%>"><%=item %></option><%}%> 
        </select> 
       </td> 
       <td><input type="submit" value="Go" name="Go"></td> 
     </tr> 
     </table> 
        </form> 
        </body> 
     </html> 

和f_m.java

String gender = request.getParameter("sex"); 
String age = request.getParameter("age"); 
if(request.getParameter("Go") != null){ 
    // i want to go to f.jsp to print the table 
} 
+0

要修复错误,您可以重命名变量,但应该用JSTL重写它。 – 2014-10-27 13:41:54

+0

500意味着你有一些服务器错误。究竟说错误是什么?检查日志 – Tarlog 2014-10-27 13:59:00

+0

@RomanC你是指哪些变量? – user1737422 2014-10-27 13:59:58

回答

0

你必须设置请求转发给jsp之前,servlet中的属性。我想这可能是这样的:

// prepare the attributes 
int ageList = new int[100]; 
int sexList = new String[]{"m", "f"}; 
for (int i=0; i<100; i++) { ageList[i] = i + 1; } 

// put them in request 
request.setAttribute("ageList", ageList); 
request.setAttribute("sexList", sexList); 

// and forward to the jsp. 
request.getRequestDispatcher("f.jsp").forward(request, response); 
0

你有两个名单:性别和年龄。当你试图填充选择时,你问:

<%for(String item : sexList) { %> 

在哪里宣布你的sexList?可能是你想通过性别列表迭代?我的意思是,尝试:

<%for(String item : sex) { %>

与同为第二个列表:

<%for (String item : age) {%> 

你必须更明确什么是你想在你的servlet做。我假设你的主要范围是根据两个列表填充两个选择:性别和年龄。在此之后,您需要选择两个值并将它们传递给servlet,servlet将根据这两个值调用数据库,并从表中返回一行。在此之后,你想在你的f.jsp中打印这一行。如果是这样,从选择获取值(和填充他们的权利,就像我下面提到)在servlet:

String sex = request.getParameter("sex"); 
String age = request.getParameter("age"); 

//method to call database, i.e List<Persons> list = DBHelper.checkSomething(sex, age); 

if(...){ 
RequestDispatcher rd = request.getRequestDispatcher("f.jsp"); 
request.setAttribute("list", list); 
rd.forward(request, response); 
} 

我强烈建议您根据视角的打印数据。如果你想了解更多,请阅读MVC模式。