Dear all,
Below is my jsp page which print the pages numbering with previous and next when applicable; this code works fine but in order to improve the jsp page I thought it will look better if I was calling a java class or method? Any advice?
thanks
Vero
<%@ page import="java.sql.*, Mypackage.*" %>
<html xmlns=" lang="en-US" xml:lang="en-US">
<head>
<title></title>
</head>
<body>
<%
String url = application.getInitParameter("dbUrl");
String pwd = application.getInitParameter("dbPwd");
String user = application.getInitParameter("dbUser");
Connection conn = ConnectionManager.getConnection(url, user, pwd);
PreparedStatement prep = conn.prepareStatement("select count(*) as rowcount from customer");
ResultSet r = prep.executeQuery();
r.next();
int rowcount = r.getInt("rowcount") ;
r.close() ;
int records=5; //number of records displayed per page
int numberOfPages; //number of pages
int thispage; //current page number
int prev_page; //previous page
int next_page; //next page
if (rowcount <= records)
{
numberOfPages = 1;
}else{
if ((rowcount%records)==0)
{
numberOfPages=(rowcount/records);
}else{
numberOfPages=(rowcount/records)+1;
}
}
if((numberOfPages * records) >= rowcount)
{
numberOfPages++;
}
int currentRs;
String pt = request.getParameter("mv");
if (pt == null)
{
currentRs = 0;
thispage = 1;
prev_page = 0;
}else{
thispage = Integer.parseInt((String)pt);
currentRs = records * (thispage -1);
prev_page =thispage-1;
}
int cpage = numberOfPages-1;
out.println(" ");
if(prev_page >= 1)
{
out.println("<a href=test.jsp?mv=" + prev_page + " class=\"t\"><img border=\"0\" src=\"../images/back_arrow.gif\"> Previous</a>");
}
for(int p=1; p < numberOfPages; p++)
{
if(p == thispage)
{
out.println("<font color=\"#9999CC\" size=\"+2\">" + p + "</font>");
}else{
out.println("<a href=test.jsp?mv=" + p + ">" + p + "</a>");
}
}
if(cpage>=1)
{
next_page=thispage+1;
}else {
next_page=0;
}
if(thispage != cpage)
{
out.println("<a href=test.jsp?mv=" + next_page + " class=\"t\">Next <img border=\"0\" src=\"../images/more_arrow.gif\"></a>");
}//else{}
PreparedStatement prep1 = conn.prepareStatement("select company_name, contact_name, address, address2, city, pcode, country, phone, fax, email, website from customer order by company_name limit " + currentRs + "," + records);
ResultSet rs = prep1.executeQuery();
while (rs.next()) {
String fax = rs.getString("fax");
String email = rs.getString("email");
String website = rs.getString("website");
String charname = rs.getString("company_name");
%>
<table width="100%" border="0">
<tr class="display1">
<td><%= rs.getString("company_name") %>
</td>
</tr>
<%
if (rs.getString("contact_name").equals("")){} else { %>
<tr class="display3">
<td>Contact Name: <%= rs.getString("contact_name") %>
</td>
</tr>
<% } %>
<tr class="display3">
<td>Address: <%= rs.getString("address") %>,
<%
if (rs.getString("address2").equals("")){} else { %>
<%=rs.getString("address2") %>, <% } %>
<%= rs.getString("city") %>,<%= rs.getString("pcode") %> - <%= rs.getString("country") %>
</td>
</tr>
<tr class="display3">
<td>Telephone: <%= rs.getString("phone") %>
<td>
</tr>
<% if (fax.equals("")) {} else { %>
<tr class="display3">
<td>Fax: <%= rs.getString("fax") %>
</td>
</tr>
<% }
if (email.equals("")) {} else { %>
<tr class="display3">
<td>Email: <a href= "mailto:<%= rs.getString("email") %>"> <%= rs.getString("email") %></a>
</td>
</tr>
<% }
if (website.equals("")) {} else { %>
<tr class="display3">
<td>Website: <a href= " website %>" onClick="window.open(' website %>', 'newWin', 'toolbar=yes,location=yes,directories=no,status=no, menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=800,height=460');return false;"> <%= website %></a>
</td>
</tr>
<% } %>
<tr>
<td align="right"><img src="../images/top_arrow.gif" border="no"> <a href="#top" style="text-decoration:none"><font size="1">back to top</font></a>
</td>
</tr>
</table>
<% }//end while
conn.close();
%>
<!-- Repeat as required -->
<!-- End Page Layout -->
<!-- Trailing Footer -->
<hr width="100%"/>
<center class="copyright"><i>© Copyright SensoryNet 2004</i></center>
</body>
</html>
Below is my jsp page which print the pages numbering with previous and next when applicable; this code works fine but in order to improve the jsp page I thought it will look better if I was calling a java class or method? Any advice?
thanks
Vero
<%@ page import="java.sql.*, Mypackage.*" %>
<html xmlns=" lang="en-US" xml:lang="en-US">
<head>
<title></title>
</head>
<body>
<%
String url = application.getInitParameter("dbUrl");
String pwd = application.getInitParameter("dbPwd");
String user = application.getInitParameter("dbUser");
Connection conn = ConnectionManager.getConnection(url, user, pwd);
PreparedStatement prep = conn.prepareStatement("select count(*) as rowcount from customer");
ResultSet r = prep.executeQuery();
r.next();
int rowcount = r.getInt("rowcount") ;
r.close() ;
int records=5; //number of records displayed per page
int numberOfPages; //number of pages
int thispage; //current page number
int prev_page; //previous page
int next_page; //next page
if (rowcount <= records)
{
numberOfPages = 1;
}else{
if ((rowcount%records)==0)
{
numberOfPages=(rowcount/records);
}else{
numberOfPages=(rowcount/records)+1;
}
}
if((numberOfPages * records) >= rowcount)
{
numberOfPages++;
}
int currentRs;
String pt = request.getParameter("mv");
if (pt == null)
{
currentRs = 0;
thispage = 1;
prev_page = 0;
}else{
thispage = Integer.parseInt((String)pt);
currentRs = records * (thispage -1);
prev_page =thispage-1;
}
int cpage = numberOfPages-1;
out.println(" ");
if(prev_page >= 1)
{
out.println("<a href=test.jsp?mv=" + prev_page + " class=\"t\"><img border=\"0\" src=\"../images/back_arrow.gif\"> Previous</a>");
}
for(int p=1; p < numberOfPages; p++)
{
if(p == thispage)
{
out.println("<font color=\"#9999CC\" size=\"+2\">" + p + "</font>");
}else{
out.println("<a href=test.jsp?mv=" + p + ">" + p + "</a>");
}
}
if(cpage>=1)
{
next_page=thispage+1;
}else {
next_page=0;
}
if(thispage != cpage)
{
out.println("<a href=test.jsp?mv=" + next_page + " class=\"t\">Next <img border=\"0\" src=\"../images/more_arrow.gif\"></a>");
}//else{}
PreparedStatement prep1 = conn.prepareStatement("select company_name, contact_name, address, address2, city, pcode, country, phone, fax, email, website from customer order by company_name limit " + currentRs + "," + records);
ResultSet rs = prep1.executeQuery();
while (rs.next()) {
String fax = rs.getString("fax");
String email = rs.getString("email");
String website = rs.getString("website");
String charname = rs.getString("company_name");
%>
<table width="100%" border="0">
<tr class="display1">
<td><%= rs.getString("company_name") %>
</td>
</tr>
<%
if (rs.getString("contact_name").equals("")){} else { %>
<tr class="display3">
<td>Contact Name: <%= rs.getString("contact_name") %>
</td>
</tr>
<% } %>
<tr class="display3">
<td>Address: <%= rs.getString("address") %>,
<%
if (rs.getString("address2").equals("")){} else { %>
<%=rs.getString("address2") %>, <% } %>
<%= rs.getString("city") %>,<%= rs.getString("pcode") %> - <%= rs.getString("country") %>
</td>
</tr>
<tr class="display3">
<td>Telephone: <%= rs.getString("phone") %>
<td>
</tr>
<% if (fax.equals("")) {} else { %>
<tr class="display3">
<td>Fax: <%= rs.getString("fax") %>
</td>
</tr>
<% }
if (email.equals("")) {} else { %>
<tr class="display3">
<td>Email: <a href= "mailto:<%= rs.getString("email") %>"> <%= rs.getString("email") %></a>
</td>
</tr>
<% }
if (website.equals("")) {} else { %>
<tr class="display3">
<td>Website: <a href= " website %>" onClick="window.open(' website %>', 'newWin', 'toolbar=yes,location=yes,directories=no,status=no, menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=800,height=460');return false;"> <%= website %></a>
</td>
</tr>
<% } %>
<tr>
<td align="right"><img src="../images/top_arrow.gif" border="no"> <a href="#top" style="text-decoration:none"><font size="1">back to top</font></a>
</td>
</tr>
</table>
<% }//end while
conn.close();
%>
<!-- Repeat as required -->
<!-- End Page Layout -->
<!-- Trailing Footer -->
<hr width="100%"/>
<center class="copyright"><i>© Copyright SensoryNet 2004</i></center>
</body>
</html>