Tomcat为何先执行jsp代码再解析HTML语法? 应该是你的getList()方法的问题吧?检查一下该写的<tr><td></td></tr>都写了吧?不该写得不多吧? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 执行完后的HTML代码是怎样的?贴出来看看,应该是你getList()方法的问题 肯定是你的程序有问题,jsp在编译的时候先转换成sevlet这个java文件,既然是个java文件,就是按顺序执行的。 关于jsp文件的编译过程可参看specification中文问题搜索一下本版,一大堆 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> 序号 </td> <td class="Title" nowrap> 名称 </td> <td class="Title" nowrap> 时间 </td> <td class="Title" nowrap> 数字 </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> 1 </nobr></td>\r"+ "<td><nobr> Wang </nobr></td>\r"+ "<td><nobr> 2003-04-24 </nobr></td>\r"+ "<td><nobr>120</nobr></td>\r"+ "</tr>");}就是这样的,显示后的源代码变成了:<tr class="Normal"><td><nobr> 1 </nobr></td><td><nobr> Wang </nobr></td><td><nobr> 2003-04-24 </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> 序号 </td> <td class="Title" nowrap> 名称 </td> <td class="Title" nowrap> 时间 </td> <td class="Title" nowrap> 数字 </td> </tr></table></body></html> 就是以上这样的,我试过,不只是getList,所有jsp代码,都是先执行了,查看源文件时都是jsp的在最前边,然后是<html>....才开始谢谢大家 是不是说在类中out.print()是打在了页面的起始部位而不是当前包含jsp的部分? com.tedasoft.example.example怎么是个servlet?第一次见到这种servlet实例化的用法。。 它是个servlet,以前在weblogic上这样用的,比较省力的解决办法。一直运行正常的。所以我猜想可能在Tomcat中,out.print方法把字符打在了response页面的最起始处,不知是否这样,该如何解决呢? 你把SERVLET当做实例来用?!以前没见过这种用法(应该不提倡的吧)你把你SERVLET中的putnew方法放到JAVABEAN中试试看啊 倒是第一次听说这样的事,这样,Tomcat目录的work子目录下存放有这个jsp页面编译时的java源代码,你找找看,把它贴出来给大家看看. 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> 搴忓彿 "); out.write("</td>\r\n "); out.write("<td class=\"Title\" nowrap> 鍚嶇О "); out.write("</td>\r\n "); out.write("<td class=\"Title\" nowrap> 鏃堕棿 "); out.write("</td>\r\n "); out.write("<td class=\"Title\" nowrap> 鏁板瓧 "); 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); } }} 我知道了, 估计是你的代码中用了 PrintWriter out =response.getWriter(); 的问题, 建议你该一下 putnew 方法, 不要在里面之间打印, 而是返回一个 String, 然后在页面上之间使用 out.write(pg.putnew(request,response)); 输出.试试看吧. sorry, 错字太多建议你改一下 putnew 方法, 不要在里面直接打印 glassprogrammer() ,谢谢你,我也想到返回一个String了,可是我又担心会不会溢出,因为真正使用时是数据库中检索出来的数据列表,搞不好就有近万行,而且有N个字段,这样即使不溢出,速度也会很慢吧。我想会不会是按这样getWriter打印,会打在页面的最开始处,有没有打印风格之类参数或者其他方法是打在当前位置的呢?谢谢!!! 几种方案:1)Bean 不要继承 HttpServlet 试试2)返回字符串3)返回一个集合, 在页面中循环打印, 比较推荐 返回什么集合?resultset吗?大哥能不能指点一下,现在最好能在改动少的情况下用,新的编码再用标准的不迟,请用不继承HttpServlet,继承哪个也可以呢? 那就回到了在Servlet总输入HTML的做法了。pg.getList 中输出你全部的内容。 那就用一个方法:在putnew的参数中把页面的 out 对象作为一个参数传进去, 直接用这个 out 打印试试, 它的类型是JspWriter 忘了说,就是用JspWriter的方法!再次感谢glassprogrammer() Hibernate 调用Tomcat的连接池出错 大家都用哪些框架开发WEB? session 丢失的问题(tomcat) struts 1 如何实现上传未知个数个文件? j2ee视频教程免费奉送 做毕业设计碰到问题 求 pdf格式的《jsp深入编程》 ,散分! 用JavaMail阅读邮件的时候,怎样才能知道邮件服务器上有几个文件夹? 我用的tomcat web server!测试已经通过!但是为什么这个jsp+bean老是报这个错误!我真的没有办法了!你出手吧!大虾 求助,tomcat无法启动 谁能给俺提供几个JSP的好网站?万分感激 JSP运行时提示错误?请指导!
贴出来看看,应该是你getList()方法的问题
中文问题搜索一下本版,一大堆
<%@ 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> 序号 </td>
<td class="Title" nowrap> 名称 </td>
<td class="Title" nowrap> 时间 </td>
<td class="Title" nowrap> 数字 </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> 1 </nobr></td>\r"+
"<td><nobr> Wang </nobr></td>\r"+
"<td><nobr> 2003-04-24 </nobr></td>\r"+
"<td><nobr>120</nobr></td>\r"+
"</tr>");
}
就是这样的,显示后的源代码变成了:
<tr class="Normal">
<td><nobr> 1 </nobr></td>
<td><nobr> Wang </nobr></td>
<td><nobr> 2003-04-24 </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> 序号 </td>
<td class="Title" nowrap> 名称 </td>
<td class="Title" nowrap> 时间 </td>
<td class="Title" nowrap> 数字 </td>
</tr></table>
</body>
</html>
谢谢大家
第一次见到这种servlet实例化的用法。。
以前没见过这种用法(应该不提倡的吧)
你把你SERVLET中的putnew方法放到JAVABEAN中试试看啊
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> 搴忓彿 ");
out.write("</td>\r\n ");
out.write("<td class=\"Title\" nowrap> 鍚嶇О ");
out.write("</td>\r\n ");
out.write("<td class=\"Title\" nowrap> 鏃堕棿 ");
out.write("</td>\r\n ");
out.write("<td class=\"Title\" nowrap> 鏁板瓧 ");
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);
}
}
}
谢谢!!!
1)Bean 不要继承 HttpServlet 试试
2)返回字符串
3)返回一个集合, 在页面中循环打印, 比较推荐
pg.getList 中输出你全部的内容。