在我的数据库中,表名是 abc ,它只包含一行,如下所示:
F S T FO FI
10/2=? 2 3 5 1
其中第一列名称= F, 第二列名称是S, 第三列名称是T,依此类推。
在home.jsp -----------
<form action="NewServlet1" method="post">
Enter Table Name :<input type="text" name="table">
<input type="submit" value="Display">
</form>
在NewServ1中的doPost ---------
String tb=request.getParameter("table");
ResultSet rs=st.executeQuery("Select * from "+tb);
while (rs.next()) {
rs.getString("F");
rs.getString("S");
rs.getString("T");
rs.getString("FO");
rs.getString("FI");
}
request.setAttribute("fi", "F");
request.setAttribute("fi1", "S");
request.setAttribute("fi2", "T");
request.setAttribute("fi3", "FO");
request.setAttribute("fi4", "FI");
request.getRequestDispatcher("result.jsp").forward(request, response);
result.jsp中-------------------
String abc="", abc1="",abc2="",abc3="",abc4="";
abc= (String) request.getAttribute("fi");
abc1= (String) request.getAttribute("fi1");
abc2= (String) request.getAttribute("fi2");
abc3= (String) request.getAttribute("fi3");
abc4= (String) request.getAttribute("fi4");
out.println(abc);
out.println(abc1);
out.println(abc2);
out.println(abc3);
out.println(abc4);
我应该像这样在浏览器页面中获得价值
10/2=?
2
3
5
1
但我是这样的:
F S T FO FI
我哪里错了?提前谢谢.....
答案 0 :(得分:1)
在
while (rs.next()) {
rs.getString("F");
rs.getString("S");
rs.getString("T");
rs.getString("FO");
rs.getString("FI");
}
您没有存储和使用从表格中选择的值,而是为您的属性设置固定值。
request.setAttribute("fi", "F"); // fixed string F
request.setAttribute("fi1", "S"); // fixed string S
request.setAttribute("fi2", "T"); // fixed string T
request.setAttribute("fi3", "FO"); // fixed string F0
request.setAttribute("fi4", "FI"); // fixed string FI
您可以使用
之类的内容代替上述代码if (rs.next()){ //I used `if` since we expect only one row.
request.setAttribute("fi", rs.getString("F"));
request.setAttribute("fi1", rs.getString("S"));
request.setAttribute("fi2", rs.getString("T"));
request.setAttribute("fi3", rs.getString("FO"));
request.setAttribute("fi4", rs.getString("FI"));
}
request.getRequestDispatcher("result.jsp").forward(request, response);