jsp+servlet实现分页

我在做jsp+servlet数据分页的时候,无法实现前后翻页,
下面是我的分层结构

jsp
[code="java"]
<%@page contentType="text/html;charset=gbk" %>
<%@page language="java" import="java.util.*" %>
<%@ include file="inc/cmnAuthenticate.jsp" %>

<%@page language="java" import="ch05.*" %>
<%
Vector vMails = null;
int totalPage=0;
int curPage=0;
int countPerPage=10;
int pageNumber=1;
Hashtable myValues = (Hashtable)session.getAttribute(CommonConst.VIEWID_INBOXLIST);
if ( myValues != null )
{
vMails = (Vector)myValues.get("mails");
if ( vMails == null )
{
vMails = new Vector();
}
if(myValues.get("totalPage")!=null){
totalPage=((Integer)myValues.get("totalPage")).intValue();
}
if(myValues.get("curPage")!=null){
curPage=((Integer)myValues.get("curPage")).intValue();
}
}

%>



收件箱



<br> &lt;%<br> if ( session.getAttribute(&quot;errMsg&quot;) != null )<br> {<br> %&gt;<br> var sErrMsg = &quot;&lt;%=session.getAttribute(&quot;errMsg&quot;)%&gt;&quot;;<br> &lt;%<br> }<br> else<br> {<br> %&gt;<br> var sErrMsg = &quot;&quot;;<br> &lt;%<br> }<br> %&gt;<br>

<%@ include file="inc/cmnPagePart1.jsp" %>

 
你当前共有 封邮件。
0 ) { for ( int i=0; i
发件人 主题 发信时间
没有任何邮件!
 
共 页,当前是第 页   0 ) { %> 首页   前一页                   后一页   末页                  

<%@ include file="inc/cmnPagePart2.jsp" %>
[/code]

servlet
[code="java"]/*

  • This product includes software developed by the
  • Apache Software Foundation (http://www.apache.org/). */ package ch05.controller;

import java.io.*;
import java.util.Hashtable;

import javax.servlet.*;
import javax.servlet.http.*;

import ch05.*;
import ch05.module.*;

/**

  • 针对收件箱页面的Servlet
  • @author Administrator
  • @version 1.0
    /
    public class Inbox extends HttpServlet implements java.io.Serializable
    {
    /
    *

    • */ private static final long serialVersionUID = 1L;

    public void doGet ( HttpServletRequest request,
    HttpServletResponse response )
    throws ServletException, IOException
    {

    //设置提交表单的中文编码
    request.setCharacterEncoding("GBK");
    
    HttpSession mySession = request.getSession(true);
    
    //清空错误消息
    mySession.setAttribute("errMsg","");
    
    //是否非法进入本页面
    if ( mySession.getAttribute("username") == null )
    {
        response.sendRedirect("/ch05/login.jsp");
        return;
    }
    //是否进入默认页面
    if ( !request.getParameterNames().hasMoreElements() )
    {
        //设置session中的页面值域
        mySession.setAttribute(CommonConst.VIEWID_INBOXLIST, new Hashtable() );
    
        //取得最新邮件
        MInbox mInbox = new MInbox();
        boolean bGetResult = mInbox.getNewestMail( mySession );
    
        String sGotoPage = request.getParameter("gotoPageNo");
    
    Hashtable myValues = (Hashtable)mySession.getAttribute(ch05.CommonConst.VIEWID_INBOXLIST);
    
    myValues.put( "curPage", new Integer(sGotoPage) );
    
        mySession.setAttribute("curPage","inbox");
        response.sendRedirect("/ch05/inbox.jsp");
        return;
    
    }
    
    //得到用户输入信息
    String sMailIndex = request.getParameter("mailIndex");
    String sMailOption = request.getParameter("mailOption");
    
    //如果用户是提交表单
    if ( sMailIndex != null && sMailIndex.length() > 0 )
    {
        //设置session中的详细页面值域
        mySession.setAttribute(CommonConst.VIEWID_INBOXDETAIL, new Hashtable() );
    
        //获得对应的邮件信息
        MInbox mInbox = new MInbox();
        boolean bGetResult = mInbox.getDetailMail( mySession, sMailIndex );
    
        if ( bGetResult )
        {
            response.sendRedirect("/ch05/inboxDetail.jsp");
        }
        else
        {
            response.sendRedirect("/ch05/inbox.jsp");
        }
    }
    //如果用户是从详细页面迁移过来的
    else if ( sMailOption != null && sMailOption.length() > 0 )
    {
        String sSender = request.getParameter("sender");
        String sSendTime = request.getParameter("sendTime");
        String sSubject = request.getParameter("subject");
        String sContent = request.getParameter("content");
        //删除邮件
        if ( sMailOption.equals("delete") )
        {
            MInbox mInbox = new MInbox();
            boolean bDeleteResult = mInbox.deleteMail( mySession, sSender, sSendTime );
            mInbox.getNewestMail( mySession );
            response.sendRedirect("/ch05/inbox.jsp");
            return;
        }
        //回复邮件
        else if ( sMailOption.equals("reply") )
        {
            //设置session中的写邮件页面值域
            mySession.setAttribute(CommonConst.VIEWID_SENDBOXDETAIL, new Hashtable() );
            MInbox mInbox = new MInbox();
            boolean bReplyResult = mInbox.replyMail( mySession, sSender, sSubject, sContent );
            mySession.setAttribute("curPage","composite");
            response.sendRedirect("/ch05/composite.jsp");
            return;
        }
        try {
            MInbox mInbox=new MInbox();
            mInbox.gotoPage(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //如果用户非法进入这个页面
    else
    {
        response.sendRedirect("/ch05/login.jsp");
    }
    

    }

    public void doPost ( HttpServletRequest request,
    HttpServletResponse response )
    throws ServletException, IOException
    {
    doGet( request, response );
    }
    }[/code]

数据库处理
[code="java"]/*

  • This product includes software developed by the
  • Apache Software Foundation (http://www.apache.org/). */ package ch05.module;

import java.sql.*;
import java.util.*;
import java.text.*;

import javax.mail.*;
import javax.mail.internet.*;

import javax.servlet.http.*;

import ch05.*;

/**

  • 针对收件箱页面的后台处理类
  • @author Administrator
  • @version 1.0
    */
    public class MInbox
    {
    public boolean getNewestMail ( HttpSession mySession )
    {
    //设置用户信息用
    Hashtable myValues = (Hashtable)mySession.getAttribute(CommonConst.VIEWID_INBOXLIST);

    String sHost = "";
    String sUser = "";
    String sPass = "";
    
    //尝试查找用户邮箱设置
    try
    {
        //载入SQLServer的JDBC驱动类
        Class.forName(CommonConst.DB_DRIVER_CLASSNAME);
        //获得数据库连接
        Connection conn = DriverManager.getConnection( CommonConst.DB_CONN_STRING );
    
        Statement stmt = null;
        ResultSet rs = null;
    
        String sUsername = (String)mySession.getAttribute("username");
    
        try
        {
            //设置数据库的rollback点
            conn.setAutoCommit(false);
    
            //检查数据库中是否已经有该用户的邮箱设置
            stmt = conn.createStatement();
    
            //执行SQL语句
            String sQuery = "select * from mail_setting where username='" + sUsername + "'";
            rs = stmt.executeQuery( sQuery );
            if (rs.next() )
            {
                sHost  = rs.getString(2);
                sUser = rs.getString(3);
                sPass = rs.getString(4);
            }
            else
            {
                mySession.setAttribute("errMsg","请首先进行邮箱设置!");
                return false;
            }
    
            //尝试连接Pop3服务器
            Properties props = System.getProperties();
            Session session = Session.getDefaultInstance(props, null);
    
            //获得收件箱
            Store store = session.getStore("pop3");
            store.connect( sHost, sUser, sPass);
    
            //获得Folder,并按照只读打开
            Folder folder = store.getFolder( "INBOX" );
            folder.open( Folder.READ_WRITE );
    
            //获得所有邮件,并依次获取内容
            Message messages[] = folder.getMessages();
    
            //往数据库中登录
            for ( int i=0; i<messages.length; i++ )
            {
                //只要不是html格式,大概没有乱码问题。。。
                String sFrom = MailContent.getFrom( messages[i].getFrom()[0].toString() );
                String sTo   = MailContent.getFrom( messages[i].getRecipients( Message.RecipientType.TO )[0].toString() );
                String sDate = (new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")).format(messages[i].getSentDate());
                String sSubject = messages[i].getSubject();
                String sContent = "";
                //如果纯文本邮件内容
                if ( messages[i].getContent() instanceof String )
                {
                    sContent = (String) messages[i].getContent();
                }
                //如果HTML格式邮件内容
                else
                {
                    sContent = ((MimeMultipart)messages[i].getContent()).getBodyPart(0).getContent().toString();
                }
    
                //插入数据库
                String sUpdateQuery = "insert into inbox values('" + sUsername + "', '"
                                                                   + sFrom + "', '"
                                                                   + sTo + "', '"
                                                                   + sDate + "', '"
                                                                   + sSubject + "', '"
                                                                   + sContent + "', '0') ";
                stmt.executeUpdate( sUpdateQuery );
            }
    
            conn.commit();
            conn.setAutoCommit(true);
    
            //删除服务器上的邮件
            for ( int i=0; i<messages.length; i++ )
            {
                messages[i].setFlag( Flags.Flag.DELETED, true );
            }
            folder.close(true);
            store.close();
    
            //重新获取数据库中的所有邮件,并表示在页面上
            Vector vMails = new Vector();
            sQuery = "select * from inbox where username='" + sUsername + "' order by send_time desc";
            rs = stmt.executeQuery( sQuery );
            while ( rs.next() )
            {
                String sFrom    = rs.getString(2);
                String sTo      = rs.getString(3);
                String sDate    = rs.getString(4);
                String sSubject = rs.getString(5);
                String sContent = rs.getString(6);
                String sReadFlg = rs.getString(7);
    
                MailContent mc = new MailContent();
                mc.setSender(sFrom);
                mc.setReceiver(sTo);
                mc.setSendTime(sDate);
                mc.setSubject(sSubject);
                mc.setContent(sContent);
                mc.setReadFlg(sReadFlg);
    
                vMails.add( mc );
    
            }
            myValues.put( "mails", vMails );
    
            //计算总页数
            int countPerPage=10;
            int totalMail=vMails.size();
            int totalPage=0;
            if(totalMail%countPerPage==0){
                totalPage=totalMail/countPerPage;
            }
            else{
                totalPage=totalMail/countPerPage+1;
            }
            int curPage=0;
            myValues.put("curPage", new Integer(curPage));
            myValues.put("totalPage", new Integer(totalPage));
    
        }
        catch(Exception e)
        {
            e.printStackTrace();
            mySession.setAttribute("errMsg","查找邮件时出现错误!");
            return false;
        }
        finally
        {
            try
            {
                rs.close();
                stmt.close();
            }catch(Exception ex)
            {
            }
        }
    }catch(Exception ex)
    {
        ex.printStackTrace();
        mySession.setAttribute("errMsg","查找邮件时出现错误!");
        return false;
    }
    
    return true;
    

    }

    }

}[/code]

js
[code="java"]window.onload = windowLoad;
//---------------------------------

//页面载入时调用
function windowLoad()
{
cmnWindowLoad();
}

//查看具体邮件
function gotoMail( index )
{
form_main.mailIndex.value = index;
form_main.submit();
}
//前后翻页
function gotoPagePosition( gotoCount )
{
form_main.pageId.value = "goto";
form_main.gotoPageNo.value = gotoCount;
form_main.submit();
}[/code]

3个回答

没错误信息,又不跳转,
有两点原因:
1,js错误。
2,逻辑上的错误。

提问请把错误信息贴上来

[quote]可能在servlet里我没有调用到跳转页面的方法 [/quote]

有跳转到页面的代码:[b]response.sendRedirect("/ch05/inbox.jsp"); [/b]
调式一下
或在这句之前加上一句[code="java"]System.out.println("程序已经运行到这里了");[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问