我在oracle中有一个名为abc的表,有4条记录如下:
F S
apple mango
tiger lion
oak banana
pink orange
<form action="NewServlet" method="post">
<input type="text" name="count" value="first" style="display:none;">
<input type="submit" value="NEXT">
</form>
if(request.getParameter("count").equals("first")){// when i clicked on next button having value first
rs=st.executeQuery("SELECT * FROM abc ORDER BY 'F','S' FETCH FIRST 1 ROWS ONLY");
}// fetches apple and mango
else{
rs=st.executeQuery("SELECT * FROM abc ORDER BY 'F','S' OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY");// when i clicked on next button having value second (in display.jsp)
}// fetches tiger and lion
request.getRequestDispatcher("display.jsp").forward(request, response);
<form action="NewServlet" method="post">
<input type="text" name="count" value="second" style="display:none;">
<input type="submit" value="NEXT">
</form>
String abc="", abc1=""
abc= (String) request.getAttribute("fi");// prints apple 1st time and tiger 2nd time but 3rd and 4th record is not fetched
abc1= (String) request.getAttribute("fi1");//prints mango 1st time and lion 2nd time but 3rd and 4th record is not fetched
当我一直点击home.jsp
和display.jsp
中的NEXT按钮时,如何获取第3和第4条记录,依此类推至第100行?
答案 0 :(得分:2)
您可以使用count
变量作为请求属性传递。
例如:
NewServlet.java
String count = request.getParameter("count");
int c = 0;
String query;
if(count != null && count.matches("\\d+")){
c = Integer.parseInt(count);
query = "SELECT * FROM abc ORDER BY 'F','S' OFFSET "+String.valueOf(c)+" ROWS FETCH NEXT 1 ROWS ONLY";
}else{
query = "SELECT * FROM abc ORDER BY 'F','S' FETCH FIRST 1 ROWS ONLY";
}
request.setAttribute("count",++c);
display.jsp
<input type="text" name="count" value="<%=(Integer)request.getAttribute("count")%>" style="display:none;">
针对home.jsp
<form action="NewServlet" method="post">
<input type="submit" value="NEXT">
</form>
从count
移除home.jsp
输入,以便您第一次调用servlet
count
参数时为空。