這是我的jsp頁面無法獲取0位置表中的第一個元素,同時檢索它?
<%@page import="java.util.List"%>
<%@page import="org.hibernate.cfg.Configuration"%>
<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.SessionFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
SessionFactory sessionfactory=null;
Session Listsession=null ;
sessionfactory=new Configuration().configure().buildSessionFactory();
Listsession = sessionfactory.openSession();
String name="";
String age="";
String address="";
String phone_no="";
try
{
int c=0;
if(request.getAttribute("c")!=null)
{
c=Integer.parseInt(request.getAttribute("c").toString());
}
org.hibernate.Query query=Listsession.createQuery("select name,age,address,phone_number from paging order by auto_inc");
query.setFirstResult(c);
query.setMaxResults(10);
List check1 =query.list();
java.util.Iterator on=check1.iterator();
while(on.hasNext())
{
Object oo[]=(Object[])on.next();
name=(String)oo[0].toString().trim();
age=(String)oo[1].toString().trim();
address=(String)oo[2].toString().trim();
phone_no=(String)oo[3].toString().trim();
%>
<div align="center" style="border-bottom:1px solid #ccc "> Name <input type="text" name="text" value="<%=name %> " disabled="disabled">
<br><br>
</div>
<% }
}
catch(Exception ee){
out.print(ee.getMessage());
}
%>
<center> <img src="fgfdg.png"> </center>
<table align="center">
<tr>
<td><a href="Pagecount?c=0">First Page</a></td>
<%
int value_counter=0;
int pagecount=0;
long counters=0;
try{
org.hibernate.Query query=Listsession.createQuery("select count(*) from paging order by auto_inc");
List check1 =query.list();
java.util.Iterator on=check1.iterator();
while(on.hasNext())
{
Object oo=on.next();
counters=((Number)oo).longValue();
}}
catch(Exception e)
{
out.println(e);
}
int count=(int)counters/10;
int Counter_in_int=count*10;
double xyz=Counter_in_int-counters;
if(xyz>=0)
{
for(int i=0;i<count;i++)
{
pagecount++;
%>
<td><a href="Pagecount?c=<%=value_counter %>"><%= pagecount%></a></td>
<% value_counter=value_counter+10;} }
else
{
%>
<%
for(int i=0;i<=count;i++)
{
pagecount++;
%>
<td><a href="Pagecount?c=<%=value_counter %>"><%= pagecount%></a></td>
<% value_counter=value_counter+10;} }
%>
<td><a href="Pagecount?c=<%=value_counter-10 %>">Last Page</a></td>
</tr>
</table>
</body>
</html>
這是對應的servlet
package testingpackage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/nn"})
public class Pagecount extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
request.setAttribute("c", request.getParameter("c"));
RequestDispatcher rd=getServletContext().getRequestDispatcher("/pager.jsp");
rd.include(request, response);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
和數據庫包含30個記錄:
Name
Tom
Mary
Martha
Angelina
Joe
Gordon
Kim
Steve
Kristen
John
Mike
Smith
Daniel
Sheamus
Vince
Longbottom
Harry
Hermione
Ron
Peter
Sammy
Fitzgerald
Leonardo
Ben
Bruce
Jude
Richie
Kate
William
Ron
我得到的每一頁上的10個結果運行該頁面後。我知道在MySQL中插入數據時,我們將它插入到第一個位置(在MYSQL中索引從1開始),但是在檢索數據時,第一個inde MYSQl的x在我們的列表中位於索引0處。
所以我的問題是 -
怎麼可能上面的代碼正常工作,因爲第1頁上它是從 「湯姆」打印名稱「約翰」,即第10條從數據庫中,但現在,當我第2頁上點擊並且現在將c設置爲10,並且它再次將記錄從「Mike」打印到「Peter」,但是它應該從「john到Ron」打印,因爲john位於數組中的第10個位置。
請不要使用scriplets!看看MVC模型2:http://www.javaranch.com/journal/200603/Journal200603.jsp#a5 –
@Anton嘿,我沒有明白你給我的鏈接,你可以告訴我我可以在上面的代碼,而不是scriplets.I想出來JSTL但沒有JSTL和scriplet我怎麼能夠在JSP頁面中編寫Java代碼。 – TruePS
你在列表中有兩次「Ron」。 –