Hi,
I am sending three
java files.
If any problem let me know.
Dayanand
//UserAdminServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.rmi.RemoteException;
import javax.ejb.FinderException;
import javax.ejb.FinderException;
public class UserAdminServlet extends HttpServlet{
private HttpSession session = null;
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void forward(HttpServletRequest req,HttpServletResponse res,
String url)
throws ServletException, IOException{
res.setContentType("text/html");
RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
rd.forward(req, res);
}
public void service(HttpServletRequest req, HttpServletResponse res)throws IOException,ServletException
{
/**
* This is for.... IF have 50 records then It display 1,2,3,4,5...
*
*/
int pages=0;
/* *
* This is dummy vector only 10( 1 to 10 or 2 to 20 ..etc)
* records keep in this Vector and forward to the UserAdmin.jsp
* /
Vector dVector=null;
......
..............
/**
* All records are available in this(userVec)Vector ...everytime I take
* only 10 records from this Vector and stored in dummy Vector.
* contenetController(Write your own object for getting all records)
* is object from this I get all records.
*/
Vector userVec=contentController.getAllUserList();
/**
* Very first time call this
servlet at that moment don't think
* about ORDER(Very first time It's NULL then It 1 to 10 records
* taking from userVec and stored in dummyVec and forward to the
* UserAdmin.jsp)
*/
String order=(String)req.getParameter("ORDER");
if(order==null){
dVector=new Vector();
pages=Math.round((dVector.size())/10);
for (Enumeration e = dVector.elements() ,int i=0; e.hasMoreElements(),i++
{
UserTB usertb=(UserTB)e.elementAt(i);
dVector.addElement(usertb);
if(i<10) break;
}
req.setAttribute("USERLIST", dVector);
req.setAttribute("PAGES", ""+pages);
this.forward(req,res,"/UserAdmin.jsp");
} else {
/** This block never excute very first time.
* First time display the (0-9 records) in UserAdmin.jsp and from that page
* we put index like 1,2,3...If click 2 It display the 10 - 19 records in that(UserAdmin.jsp)
* U put <a href="/UserAdminServlet?ORDER=2">2</a> It calls this servlet take the 10(10-19)
* records from userVec and keep into the dummyVector and forward to the UserAdmin.jsp
*/
int orderInt=(int)Integer.parseInt(order);
dVector=new Vector();
pages=Math.round((dVector.size())/10);
for (Enumeration e1 = dVector.elements() ,int i1=(orderInt-1)*10; e1.hasMoreElements(),i1++
{
UserTB usertb=(UserTB)e1.elementAt(i1);
dVector.addElement(usertb);
if(i1<((orderInt)*10)) break;
}
req.setAttribute("USERLIST", dVector);
req.setAttribute("PAGES", ""+pages);
this.forward(req,res,"/UserAdmin.jsp");
}
}//service
}
//UserTB.java
public class UserTB {
private String userName=null;
private String password=null;
...
pulic void setUserName(String userName){
this.userName=userName;
}
public void setPassword(String password){
this.password=password;
}
.....
public String getUserName(){
return userName;
}
public String getPassword(){
return passWord;
}
.....
}
//UserAdmin.jsp
<%@ page import="java.util.Vector"%>
<%@ page import="java.util.Enumeration"%>
<%@ page session = "true" %>
<%
userVec=(Vector)request.getAttribute("USERLIST");
int pages=(int)Integer.parseInt((String)request.getAttribute("PAGES"));
%>
<html>
<form NAME="admin" action="" METHOD="POST" >
<table width=100% border="1" cellspacing="2" cellpadding="5" >
<tr>
<th width="20%" class="adminhdr">User Name</th>
.......
</tr>
<%
for( Enumeration e=userVec.elements() ;e.hasMoreElements();i++){
UserTB usertb = null;
String userId=null;
usertb=(UserTB)e.nextElement();
userId=(String)usertb.getUserName();
....
%>
<tr>
<td width="20%" class="admin"> <%=userId%> </td>
</tr>
<%}%>
</table><br>
<%
//Here display index like 1,2,3,4.... if U click 1 it calls the servlet and the servlet take the records(1 -10) form
//servlet and forward to the this page.
for(int i=0;i<pages;i++){
%><a href="/UserAdminServlet?ORDER=<%=(i++)"><%=(i++)%></a>
}
%>
</form>
</body>
</html>