2 qq 34791921 qq_34791921 于 2016.04.29 15:30 提问

为什么我写的servlet的第一页无法选中?

图片说明

无论怎么换页页码1都无法选中;
主要代码如下

for(int i=1;i<=pageCount;i++){
if(i==pageNow){
pw.print(""+ i + "");
}
else{
pw.print(""+ i + "");

            }
        }

4个回答

qq_34791921
qq_34791921   2016.04.29 15:31

全部的代码:
package com.ads;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class Success extends HttpServlet{
ResultSet rs = null;
PreparedStatement pstmt = null;
Connection conn = null;
public void doGet(HttpServletRequest req,HttpServletResponse res){
try{
String u = req.getParameter("uname");
String p = req.getParameter("pass");
HttpSession hs = req.getSession(true);
String val =(String)hs.getAttribute("pas");

        PrintWriter pw = res.getWriter();
        if(val==null){
            res.sendRedirect("login");

        }

        int pageSize = 3;
        int pageNow = 1;
        int rowCount = 0;
        int pageCount = 0;

        String sPageNow = req.getParameter("pageNow");
        if(sPageNow!=null){
            pageNow = Integer.parseInt(sPageNow);
        }
        pw.println("<body>");

        pw.println("<img src=Images/Penguins.jpg width=200px><br/>");
        pw.println("hello,u:"+ u + "p:" + p);
        pw.println("<a href=# >return login</a>");

        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","zhushaowei12");
        pstmt = conn.prepareStatement("select count(*) from users");
        rs = pstmt.executeQuery();
        if(rs.next()){
            rowCount = rs.getInt(1);

        }
        if(rowCount%pageSize==0){
            pageCount = rowCount/pageSize;

        }
        else{
            pageCount = rowCount/pageSize + 1;

        }
        pstmt = conn.prepareStatement("select * from (select a1.*,rownum rn from (select * from users) a1 where rownum<=?)where rn>=?");
        pstmt.setInt(1,pageSize*pageNow);
        pstmt.setInt(2,pageSize*(pageNow-1)+1);
        rs = pstmt.executeQuery();

        pw.println("<table border=1>");
        pw.println("<tr><td>id</td><td>username</td><td>password</td><td>email</td><td>grade</td>");

        while(rs.next()){
            pw.println("<tr>");
            pw.println("<td>"+rs.getInt(1)+"</td>");
            pw.println("<td>"+rs.getString(2)+"</td>");
            pw.println("<td>"+rs.getString(3)+"</td>");
            pw.println("<td>"+rs.getString(4)+"</td>");
            pw.println("<td>"+rs.getInt(5)+"</td>");
            pw.println("</tr>");

        }

        pw.println("</table");
        for(int i=1;i<=pageCount;i++){
            if(i==pageNow){
                pw.print("<a href=success?pageNow="+ i +">"+ i + "</a>");
            }
            else{
                pw.print("<a href=success?pageNow="+ i +">"+ i + "</a>");

            }
        }

        pw.println("</body>");




    }
    catch(Exception e){
        e.printStackTrace();

    }
}

public void doPost(HttpServletRequest req,HttpServletResponse res){
    this.doGet(req,res);

}

}

qq_34791921
qq_34791921   2016.04.29 15:32

全部的代码:
package com.ads;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class Success extends HttpServlet{
ResultSet rs = null;
PreparedStatement pstmt = null;
Connection conn = null;
public void doGet(HttpServletRequest req,HttpServletResponse res){
try{
String u = req.getParameter("uname");
String p = req.getParameter("pass");
HttpSession hs = req.getSession(true);
String val =(String)hs.getAttribute("pas");

        PrintWriter pw = res.getWriter();
        if(val==null){
            res.sendRedirect("login");

        }

        int pageSize = 3;
        int pageNow = 1;
        int rowCount = 0;
        int pageCount = 0;

        String sPageNow = req.getParameter("pageNow");
        if(sPageNow!=null){
            pageNow = Integer.parseInt(sPageNow);
        }
        pw.println("<body>");

        pw.println("<img src=Images/Penguins.jpg width=200px><br/>");
        pw.println("hello,u:"+ u + "p:" + p);
        pw.println("<a href=# >return login</a>");

        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","zhushaowei12");
        pstmt = conn.prepareStatement("select count(*) from users");
        rs = pstmt.executeQuery();
        if(rs.next()){
            rowCount = rs.getInt(1);

        }
        if(rowCount%pageSize==0){
            pageCount = rowCount/pageSize;

        }
        else{
            pageCount = rowCount/pageSize + 1;

        }
        pstmt = conn.prepareStatement("select * from (select a1.*,rownum rn from (select * from users) a1 where rownum<=?)where rn>=?");
        pstmt.setInt(1,pageSize*pageNow);
        pstmt.setInt(2,pageSize*(pageNow-1)+1);
        rs = pstmt.executeQuery();

        pw.println("<table border=1>");
        pw.println("<tr><td>id</td><td>username</td><td>password</td><td>email</td><td>grade</td>");

        while(rs.next()){
            pw.println("<tr>");
            pw.println("<td>"+rs.getInt(1)+"</td>");
            pw.println("<td>"+rs.getString(2)+"</td>");
            pw.println("<td>"+rs.getString(3)+"</td>");
            pw.println("<td>"+rs.getString(4)+"</td>");
            pw.println("<td>"+rs.getInt(5)+"</td>");
            pw.println("</tr>");

        }

        pw.println("</table");
        for(int i=1;i<=pageCount;i++){
            if(i==pageNow){
                pw.print("<a href=success?pageNow="+ i +">"+ i + "</a>");
            }
            else{
                pw.print("<a href=success?pageNow="+ i +">"+ i + "</a>");

            }
        }

        pw.println("</body>");




    }
    catch(Exception e){
        e.printStackTrace();

    }
}

public void doPost(HttpServletRequest req,HttpServletResponse res){
    this.doGet(req,res);

}

}

qq_34791921
qq_34791921   2016.04.29 15:31

全部的代码:
package com.ads;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class Success extends HttpServlet{
ResultSet rs = null;
PreparedStatement pstmt = null;
Connection conn = null;
public void doGet(HttpServletRequest req,HttpServletResponse res){
try{
String u = req.getParameter("uname");
String p = req.getParameter("pass");
HttpSession hs = req.getSession(true);
String val =(String)hs.getAttribute("pas");

        PrintWriter pw = res.getWriter();
        if(val==null){
            res.sendRedirect("login");

        }

        int pageSize = 3;
        int pageNow = 1;
        int rowCount = 0;
        int pageCount = 0;

        String sPageNow = req.getParameter("pageNow");
        if(sPageNow!=null){
            pageNow = Integer.parseInt(sPageNow);
        }
        pw.println("<body>");

        pw.println("<img src=Images/Penguins.jpg width=200px><br/>");
        pw.println("hello,u:"+ u + "p:" + p);
        pw.println("<a href=# >return login</a>");

        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","zhushaowei12");
        pstmt = conn.prepareStatement("select count(*) from users");
        rs = pstmt.executeQuery();
        if(rs.next()){
            rowCount = rs.getInt(1);

        }
        if(rowCount%pageSize==0){
            pageCount = rowCount/pageSize;

        }
        else{
            pageCount = rowCount/pageSize + 1;

        }
        pstmt = conn.prepareStatement("select * from (select a1.*,rownum rn from (select * from users) a1 where rownum<=?)where rn>=?");
        pstmt.setInt(1,pageSize*pageNow);
        pstmt.setInt(2,pageSize*(pageNow-1)+1);
        rs = pstmt.executeQuery();

        pw.println("<table border=1>");
        pw.println("<tr><td>id</td><td>username</td><td>password</td><td>email</td><td>grade</td>");

        while(rs.next()){
            pw.println("<tr>");
            pw.println("<td>"+rs.getInt(1)+"</td>");
            pw.println("<td>"+rs.getString(2)+"</td>");
            pw.println("<td>"+rs.getString(3)+"</td>");
            pw.println("<td>"+rs.getString(4)+"</td>");
            pw.println("<td>"+rs.getInt(5)+"</td>");
            pw.println("</tr>");

        }

        pw.println("</table");
        for(int i=1;i<=pageCount;i++){
            if(i==pageNow){
                pw.print("<a href=success?pageNow="+ i +">"+ i + "</a>");
            }
            else{
                pw.print("<a href=success?pageNow="+ i +">"+ i + "</a>");

            }
        }

        pw.println("</body>");




    }
    catch(Exception e){
        e.printStackTrace();

    }
}

public void doPost(HttpServletRequest req,HttpServletResponse res){
    this.doGet(req,res);

}

}

qq_34791921
qq_34791921   2016.04.29 15:32

全部的代码:
package com.ads;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class Success extends HttpServlet{
ResultSet rs = null;
PreparedStatement pstmt = null;
Connection conn = null;
public void doGet(HttpServletRequest req,HttpServletResponse res){
try{
String u = req.getParameter("uname");
String p = req.getParameter("pass");
HttpSession hs = req.getSession(true);
String val =(String)hs.getAttribute("pas");

        PrintWriter pw = res.getWriter();
        if(val==null){
            res.sendRedirect("login");

        }

        int pageSize = 3;
        int pageNow = 1;
        int rowCount = 0;
        int pageCount = 0;

        String sPageNow = req.getParameter("pageNow");
        if(sPageNow!=null){
            pageNow = Integer.parseInt(sPageNow);
        }
        pw.println("<body>");

        pw.println("<img src=Images/Penguins.jpg width=200px><br/>");
        pw.println("hello,u:"+ u + "p:" + p);
        pw.println("<a href=# >return login</a>");

        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","zhushaowei12");
        pstmt = conn.prepareStatement("select count(*) from users");
        rs = pstmt.executeQuery();
        if(rs.next()){
            rowCount = rs.getInt(1);

        }
        if(rowCount%pageSize==0){
            pageCount = rowCount/pageSize;

        }
        else{
            pageCount = rowCount/pageSize + 1;

        }
        pstmt = conn.prepareStatement("select * from (select a1.*,rownum rn from (select * from users) a1 where rownum<=?)where rn>=?");
        pstmt.setInt(1,pageSize*pageNow);
        pstmt.setInt(2,pageSize*(pageNow-1)+1);
        rs = pstmt.executeQuery();

        pw.println("<table border=1>");
        pw.println("<tr><td>id</td><td>username</td><td>password</td><td>email</td><td>grade</td>");

        while(rs.next()){
            pw.println("<tr>");
            pw.println("<td>"+rs.getInt(1)+"</td>");
            pw.println("<td>"+rs.getString(2)+"</td>");
            pw.println("<td>"+rs.getString(3)+"</td>");
            pw.println("<td>"+rs.getString(4)+"</td>");
            pw.println("<td>"+rs.getInt(5)+"</td>");
            pw.println("</tr>");

        }

        pw.println("</table");
        for(int i=1;i<=pageCount;i++){
            if(i==pageNow){
                pw.print("<a href=success?pageNow="+ i +">"+ i + "</a>");
            }
            else{
                pw.print("<a href=success?pageNow="+ i +">"+ i + "</a>");

            }
        }

        pw.println("</body>");




    }
    catch(Exception e){
        e.printStackTrace();

    }
}

public void doPost(HttpServletRequest req,HttpServletResponse res){
    this.doGet(req,res);

}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!