应该是你的getList()方法的问题吧?
检查一下该写的<tr><td></td></tr>都写了吧?不该写得不多吧?

解决方案 »

  1.   

    执行完后的HTML代码是怎样的?
    贴出来看看,应该是你getList()方法的问题
      

  2.   

    肯定是你的程序有问题,jsp在编译的时候先转换成sevlet这个java文件,既然是个java文件,就是按顺序执行的。
      

  3.   

    关于jsp文件的编译过程可参看specification
    中文问题搜索一下本版,一大堆
      

  4.   

    List.jsp:<html>
    <%@ page contentType="text/html;charset=gb2312" %>
    <jsp:useBean id="ConnedToDB" class="com.tedasoft.database.ConnectDB" scope="application" />
    <head>
    <title>示例</title>
    </head><BODY >
    <link rel="stylesheet" type="text/css" href="../../../public/default.css">
    <table cellspacing="0" frame="border" id="Tb">
    <thead>
      <tr>
        <td class="Title" nowrap>&nbsp;序号&nbsp;</td>
        <td class="Title" nowrap>&nbsp;名称&nbsp;</td>
        <td class="Title" nowrap>&nbsp;时间&nbsp;</td>
        <td class="Title" nowrap>&nbsp;数字&nbsp;</td>
      </tr>
    <%
    com.tedasoft.example.example pg=new com.tedasoft.example.example();
    pg.setConnection(ConnedToDB.getConnection());
    pg.putnew(request,response);
    %>
    </table>
    </body>
    </html>example.java:
    public void putnew(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException,Exception
    {
    PrintWriter out =response.getWriter();
    out.println("<tr class=\"Normal\">");
    out.print("<td><nobr>&nbsp;1&nbsp;</nobr></td>\r"+
     "<td><nobr>&nbsp;Wang&nbsp;</nobr></td>\r"+
     "<td><nobr>&nbsp;2003-04-24&nbsp;</nobr></td>\r"+
     "<td><nobr>120</nobr></td>\r"+
     "</tr>");
    }
    就是这样的,显示后的源代码变成了:
    <tr class="Normal">
    <td><nobr>&nbsp;1&nbsp;</nobr></td>
    <td><nobr>&nbsp;Wang&nbsp;</nobr></td>
    <td><nobr>&nbsp;2003-04-24&nbsp;</nobr></td>
    <td><nobr>120</nobr></td>
    </tr><html>
    <head>
    <title>示例</title>
    </head><BODY >
    <link rel="stylesheet" type="text/css" href="../../../public/default.css">
    <table cellspacing="0" frame="border" id="Tb">
    <thead>
      <tr>
        <td class="Title" nowrap>&nbsp;序号&nbsp;</td>
        <td class="Title" nowrap>&nbsp;名称&nbsp;</td>
        <td class="Title" nowrap>&nbsp;时间&nbsp;</td>
        <td class="Title" nowrap>&nbsp;数字&nbsp;</td>
      </tr></table>
    </body>
    </html>
      

  5.   

    就是以上这样的,我试过,不只是getList,所有jsp代码,都是先执行了,查看源文件时都是jsp的在最前边,然后是<html>....才开始
    谢谢大家
      

  6.   

    是不是说在类中out.print()是打在了页面的起始部位而不是当前包含jsp的部分?
      

  7.   

    com.tedasoft.example.example怎么是个servlet?
    第一次见到这种servlet实例化的用法。。
      

  8.   

    它是个servlet,以前在weblogic上这样用的,比较省力的解决办法。一直运行正常的。所以我猜想可能在Tomcat中,out.print方法把字符打在了response页面的最起始处,不知是否这样,该如何解决呢?
      

  9.   

    你把SERVLET当做实例来用?!
    以前没见过这种用法(应该不提倡的吧)
    你把你SERVLET中的putnew方法放到JAVABEAN中试试看啊
      

  10.   

    倒是第一次听说这样的事,这样,Tomcat目录的work子目录下存放有这个jsp页面编译时的java源代码,你找找看,把它贴出来给大家看看.
      

  11.   

    List.jsp页面编译后的java文件,还是先执行jsp再解析HTML的:
    package org.apache.jsp;import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    import org.apache.jasper.runtime.*;public class List_jsp extends HttpJspBase {
      private static java.util.Vector _jspx_includes;  public java.util.List getIncludes() {
        return _jspx_includes;
      }  public void _jspService(HttpServletRequest request, HttpServletResponse response)
            throws java.io.IOException, ServletException {    JspFactory _jspxFactory = null;
        javax.servlet.jsp.PageContext pageContext = null;
        HttpSession session = null;
        ServletContext application = null;
        ServletConfig config = null;
        JspWriter out = null;
        Object page = this;
        JspWriter _jspx_out = null;
        try {
          _jspxFactory = JspFactory.getDefaultFactory();
          response.setContentType("text/html;charset=gb2312");
          pageContext = _jspxFactory.getPageContext(this, request, response,
           null, true, 8192, true);
          application = pageContext.getServletContext();
          config = pageContext.getServletConfig();
          session = pageContext.getSession();
          out = pageContext.getOut();
          _jspx_out = out;      out.write("<html>\r\n");
          out.write("\r\n");
          com.tedasoft.database.ConnectDB ConnedToDB = null;
          synchronized (application) {
            ConnedToDB = (com.tedasoft.database.ConnectDB) pageContext.getAttribute("ConnedToDB", PageContext.APPLICATION_SCOPE);
            if (ConnedToDB == null){
              try {
                ConnedToDB = (com.tedasoft.database.ConnectDB) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "com.tedasoft.database.ConnectDB");
              } catch (ClassNotFoundException exc) {
                throw new InstantiationException(exc.getMessage());
              } catch (Exception exc) {
                throw new ServletException("Cannot create bean of class " + "com.tedasoft.database.ConnectDB", exc);
              }
              pageContext.setAttribute("ConnedToDB", ConnedToDB, PageContext.APPLICATION_SCOPE);
            }
          }
          out.write("\r\n");
          com.tedasoft.example.example example = null;
          synchronized (application) {
            example = (com.tedasoft.example.example) pageContext.getAttribute("example", PageContext.APPLICATION_SCOPE);
            if (example == null){
              try {
                example = (com.tedasoft.example.example) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "com.tedasoft.example.example");
              } catch (ClassNotFoundException exc) {
                throw new InstantiationException(exc.getMessage());
              } catch (Exception exc) {
                throw new ServletException("Cannot create bean of class " + "com.tedasoft.example.example", exc);
              }
              pageContext.setAttribute("example", example, PageContext.APPLICATION_SCOPE);
            }
          }
          out.write("\r\n");
          out.write("<head>\r\n");
          out.write("<title>绀轰緥");
          out.write("</title>\r\n");
          out.write("</head>\r\n\r\n");
          out.write("<BODY >\r\n");
          out.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"../../../public/default.css\">\r\n");
          out.write("<table cellspacing=\"0\" frame=\"border\" id=\"Tb\">\r\n");
          out.write("<thead>\r\n  ");
          out.write("<tr>\r\n    ");
          out.write("<td class=\"Title\" nowrap>&nbsp;搴忓彿&nbsp;");
          out.write("</td>\r\n    ");
          out.write("<td class=\"Title\" nowrap>&nbsp;鍚嶇О&nbsp;");
          out.write("</td>\r\n    ");
          out.write("<td class=\"Title\" nowrap>&nbsp;鏃堕棿&nbsp;");
          out.write("</td>\r\n    ");
          out.write("<td class=\"Title\" nowrap>&nbsp;鏁板瓧&nbsp;");
          out.write("</td>\r\n  ");
          out.write("</tr>\r\n");//com.tedasoft.example.example pg=new com.tedasoft.example.example();
    example.setConnection(ConnedToDB.getConnection());
    example.putnew(request,response);      out.write("\r\n");
          out.write("</table>\r\n");
          out.write("</body>\r\n");
          out.write("</html>");
        } catch (Throwable t) {
          out = _jspx_out;
          if (out != null && out.getBufferSize() != 0)
            out.clearBuffer();
          if (pageContext != null) pageContext.handlePageException(t);
        } finally {
          if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
        }
      }
    }
      

  12.   

    我知道了, 估计是你的代码中用了 PrintWriter out =response.getWriter(); 的问题, 建议你该一下 putnew 方法, 不要在里面之间打印, 而是返回一个 String, 然后在页面上之间使用 out.write(pg.putnew(request,response)); 输出.试试看吧.
      

  13.   

    sorry, 错字太多建议你改一下 putnew 方法, 不要在里面直接打印
      

  14.   

    glassprogrammer() ,谢谢你,我也想到返回一个String了,可是我又担心会不会溢出,因为真正使用时是数据库中检索出来的数据列表,搞不好就有近万行,而且有N个字段,这样即使不溢出,速度也会很慢吧。我想会不会是按这样getWriter打印,会打在页面的最开始处,有没有打印风格之类参数或者其他方法是打在当前位置的呢?
    谢谢!!!
      

  15.   

    几种方案:
    1)Bean 不要继承 HttpServlet 试试
    2)返回字符串
    3)返回一个集合, 在页面中循环打印, 比较推荐
      

  16.   

    返回什么集合?resultset吗?大哥能不能指点一下,现在最好能在改动少的情况下用,新的编码再用标准的不迟,请用不继承HttpServlet,继承哪个也可以呢?
      

  17.   

    那就回到了在Servlet总输入HTML的做法了。
    pg.getList 中输出你全部的内容。
      

  18.   

    那就用一个方法:在putnew的参数中把页面的 out 对象作为一个参数传进去, 直接用这个 out 打印试试, 它的类型是JspWriter
      

  19.   

    忘了说,就是用JspWriter的方法!再次感谢glassprogrammer()