我自己写的一个servlet里面有一段代码老是报异常,编译可以通过,但是一运行就出现异常。为了检查错误,为专门建了一个包含main函数的类,我原封不动的把代码复制到那个main函数中,结果运行一切正常,并没有问题。真是怪事啊,难道相同的代码仅仅挪个地方就会出现问题?又或者代码是在servlet中运行的机制不同。
下面是代码,其中的Web系列类都是第三方类库,不用去深究try 
               {
            
                       WebConversation wc = new WebConversation();
                       WebRequest r = new PostMethodWebRequest("");
                       WebResponse re = wc.getResponse(r);
                       WebTable webTable = re.getTables()[2]; 
                       String[][] datas = webTable.asText(); 
                       if(datas!=null)
                       {    
                       MyBean a=new MyBean();
                       a.setA(datas); 
                       }
                } catch (Exception ev) {response.sendRedirect("http://localhost/web/err.html"); return; }

解决方案 »

  1.   

    既然使用了第三方的web类库为什么还要用servlet,把异常贴出来看看
      

  2.   

    把异常贴出来,看看什么错误,你用的什么工具jbuilder吗?
      

  3.   

    就是下面这个类,我在main函数中调用没有问题,可以用JSP调用就抛异常。
    import java.text.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import com.bean.*;
    import com.meterware.httpunit.*;   
       
       public class ControlServlet extends HttpServlet 
       {      
              public void doPost(HttpServletRequest request,
                          HttpServletResponse response)
              throws IOException, ServletException
              { 
                   
                   try 
                   {
                           
                           WebConversation wc = new WebConversation();
                           WebRequest r = new PostMethodWebRequest("http://bbs.tsinghua.edu.cn/bbsodoc.php?board=Song_Visit");
                           WebResponse re = wc.getResponse(r); 
                           WebTable webTable = re.getTables()[2]; 
                           String[][] datas = webTable.asText(); 
                           System.out.println(datas[1][3]);
                           if(datas!=null)    
                           {    
                           MyBean a=new MyBean();
                           a.setA(datas); 
                           }
                    } catch (Exception ev) {  this.doPost(request,response); }
                    response.sendRedirect("http://localhost/web/view.jsp");
              }
              
              
              public void doGet(HttpServletRequest request, HttpServletResponse response)
              throws IOException, ServletException
              {
                     this.doPost(request,response);
              }
       
       }