public ArrayList DemandpaperTakeout(String emid_code,int fn) throws IOException, SQLException
  {
    ArrayList al1 = new ArrayList();
    ArrayList al2 = new ArrayList();    try
    {
      this.conn();
      pstmt = conn.prepareStatement(Constant.SQL_DemandpaperTakeout); 
      pstmt.setString(1,emid_code);
      pstmt.executeQuery();
      ResultSet rs = pstmt.executeQuery();
     
      while(rs.next())
      {
        for(int n = 1 ;n<=fn;n++)
         {
            al1.add(rs.getString(n));           
         }         
         al2.add(al1);
         al1 = new ArrayList();
      }      
    }
    catch (Exception e) {
e.printStackTrace(); } finally {
this.close();
}
    return al2;
  }
这个是我的源程序,在获得参数emid_code后,调用SQL语句进行查询,结果集会被调用获取,fn是产生的结果集的列数。
现在有一个问题,要求出现异常之后CATCH到一个JSP页面中去,
我知道的页面跳转就 public void process(HttpServlet servlet,HttpServletRequest req, HttpServletResponse rep) throws ServletException, IOException,然后 dispatcher = req.getRequestDispatcher(".url");dispatcher.forward(req, rep);但是这个里面貌似不能继承HttpServletRequest这些东西,怎么完成跳转,假设需要跳转的JSP页面是ERROR.JSP.在线等,谢谢

解决方案 »

  1.   

    晕你直接向上 throw 异常阿, 在jsp里面catch并跳转咯你在bean里面操作servlet的东西会变成反向依赖

      

  2.   

    dispatcher = req.getRequestDispatcher(url);dispatcher.forward(req, rep) 可以.
    而且跳转到的页面里也能获取到request原有的值,并不会丢失.
    你可以通过 request.setAttribut("error",e);把Exception保存下来,然后在跳转到的页面通过
    Exception e=(Exception)request.getAttribute("error") 获取到,这样你想输出错误信息当然不成问题.
      

  3.   

    支持异常向上抛, 但是最好是抛到servlet或者是Action, 在Servlet或者action 里面控制跳转吧
      

  4.   

    恩,我上一步是一片ELSE IF()其中一个就是传参进来然后获取结果集,最后抛出异常的话统一跳转到另一个JSP页面中去,但是我想这个出现异常的话跳到error.jsp这个里面去,有不想做太大的改动
      

  5.   

    dispatcher = req.getRequestDispatcher(url);dispatcher.forward(req, rep) 
    直接用的话他会说参数未定义,
    直接加入HttpServlet servlet,HttpServletRequest req, HttpServletResponse rep的话会造成参数数量传递错误
    我是新手,您能直接在CATCH中写一下么,不胜感激
      

  6.   

    public ArrayList DemandpaperTakeout(String emid_code,int fn) throws IOException, SQLException 
    你这个函数是哪里调用的? 如果不是JSP,Servlet或其子类,一般是没有request的。如果你的这个类是在Servlet中调用,请把request传递过来,比如改成这样的:
    public ArrayList DemandpaperTakeout(String emid_code,int fn,HttpServletRequest req, HttpServletResponse rep) throws IOException, SQLException {
        ....
        }catche(Exception e){
               request.getRequestDispatcher(errorPage).forward(req,rep);
        }}但如果你这个类是在servlet调用,最好还是向上抛吧,在servlet中处理这个异常,方式也一样:
    catche(Exception e){
               request.getRequestDispatcher(errorPage).forward(req,rep);
        }
      

  7.   

    把错误往上面扔public ArrayList DemandpaperTakeout(String emid_code,int fn) throws IOException, SQLException 
      { 
        ArrayList al1 = new ArrayList(); 
        ArrayList al2 = new ArrayList();     try 
        { 
          this.conn(); 
          pstmt = conn.prepareStatement(Constant.SQL_DemandpaperTakeout); 
          pstmt.setString(1,emid_code); 
          pstmt.executeQuery(); 
          ResultSet rs = pstmt.executeQuery(); 
        
          while(rs.next()) 
          { 
            for(int n = 1 ;n <=fn;n++) 
            { 
                al1.add(rs.getString(n));          
            }        
            al2.add(al1); 
            al1 = new ArrayList(); 
          }      
        } 
        catch (SQLException e) { 
    e.printStackTrace();
    throw new SQLException("message");} finally { 
    this.close(); 

        return al2; 
      } 在最上层去派发
      

  8.   

    try{
    if.......  
      }else if(Constant.OP_EXAMPAPER_CREATE.equals(op))
              {
                  //5.1 变量声明
                  UserCommon userCommon = null;
                  UserDB userDB = null;
                  String exam_code = null;
                  String emid_code = null;
                  String emne_username = null;
                  String df = null;
                  HttpSession session;
                  ArrayList al = new ArrayList();
                  ArrayList al_sjtp = new ArrayList();  
                  
                  userCommon = new UserCommon();
                  df = userCommon.getDate();
                  session = req.getSession();              
                  emne_username = (String)session.getAttribute("emne_username"); 
                  exam_code = req.getParameter("exam_code");              
                  userDB = new UserDB(Constant.DB_USERNAME,Constant.DB_PASSWORD,Constant.DB_IP,Constant.DB_DATABASE,
                  Constant.DB_PORT,Constant.DB_URL);             
                  
                  al = userDB.ExampaperTakeout(exam_code,emne_username,Constant.PARA_EXAMPAPER_FILEDNUMBER);
                  //传参,然后调用的ExamperTakeout,也就是我要修改的,将获得的结果集放在al中
                  Iterator iter   = al.iterator();
                    emid_code = (String)((ArrayList)iter.next()).iterator().next();
                    System.out.println(emid_code);                   al_sjtp = userDB.getSubjectType(exam_code ,emid_code , Constant.PARA_EXAMPAPER_SJTP_FN);
                         
                  req.setAttribute("fulltime",fulltime);    
                    req.setAttribute("al_sjtp",al_sjtp);   
                    req.setAttribute("emid_code",emid_code);   
                    req.setAttribute("exam_code",exam_code);   
                    req.setAttribute("result",al);    
                  dispatcher = req.getRequestDispatcher("../exampaper.jsp");
                  dispatcher.forward(req, rep);//跳转到exampaper中,这个JSP页面会调用结果集中的数据
    ....
            }catch (Exception e) {
              e.printStackTrace();
              dispatcher = req.getRequestDispatcher("/zz.jsp");
              dispatcher.forward(req, rep);//最后所有异常都会抛出到ZZ.JSP
            }
    我想让Takeout产生的异常抛出到error.jsp中
    public ArrayList DemandpaperTakeout(String emid_code,int fn,HttpServletRequest req, HttpServletResponse rep) 这个样子是不行的,会影响我传参的,你们说的将异常派发到最上层是派发到JSP页面中去么?如何操作呢?
      

  9.   

    应该是把异常在servlet中处理, 根据不同的exception 把相应的error message 存在request中,然后在servlet中跳转到error.jsp, 在error.jsp中取得相应的error message并显示
    这样处理应该可以的
      

  10.   

    跳转页面本身就应该从页面跳到另一个嘛,在action里加或页面catch异常再跳转吧,改动也不是很大