Topic: 做servlet遇到一个莫名其妙的问题。有源码 |
Print this page |
1.做servlet遇到一个莫名其妙的问题。有源码 | Copy to clipboard |
Posted by: goodluckwhh Posted on: 2005-11-20 15:01 下面这个servlet程序没有任何显示(数据库里绝对有内容其它模块运行都没问题,可以读和写就这个不行),也没报任何错误,我把数据库的名字故意写错它也不报错,那位大哥帮我看看,谢谢 import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.util.*; public class listServlet extends HttpServlet { public void init(ServletConfig config)throws ServletException { super.init(config); } public void destory() { super.destroy(); } public void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException { resp.setContentType("text/html"); PrintWriter out=new PrintWriter(resp.getOutputStream()); try { String url="jdbc:odbcRIVER=Microsoft Access Driver (*.mdb);DBQ=C:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\webapps\\myshop\\WEB-INF\\fanc.mdb"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn=DriverManager.getConnection(url); Statement stmt=conn.createStatement(); String strSQL="SELECT * FROM goods"; ResultSet rs=stmt.executeQuery(strSQL); //ResultSetMetaData rsmd=rs.getMetaData(); //int colCount=rsmd.getColumnCount(); //out.println(colCount); int count=1; out.println("<table border='0'>"); out.println("<tr>"); out.println("<td bgcolor='#FFCCCC'>商品名</td>"); out.println("<td bgcolor='#FFCCCC'>类型</td>"); out.println("<td bgcolor='#FFCCCC'>简介</td>"); out.println("<td bgcolor='#FFCCCC'>价格</td>"); out.println("<td bgcolor='#FFCCCC'>折扣</td>"); out.println("<td bgcolor='#FFCCCC'>编号</td>"); out.println("<td bgcolor='#FFCCCC'>数量</td>"); out.println("<td bgcolor='#FFCCCC'>操作</td>"); out.println("</tr>"); while(rs.next()) { out.println("<tr>"); out.println("<td bgcolor='#FFCC33'>"); String name=rs.getString(1); out.println(name); out.println("</td>"); out.println("<td bgcolor='#FFCC33'>"); String type=rs.getString(2); out.println(type); out.println("</td>"); out.println("<td bgcolor='#FFCC33'>"); out.println(rs.getString(3)); out.println("</td>"); out.println("<td bgcolor='#FFCC33'>"); long price=rs.getLong(4); out.println(price); out.println("</td>"); out.println("<td bgcolor='#FFCC33'>"); int off=rs.getInt(5); out.println(off); out.println("</td>"); out.println("<td bgcolor='#FFCC33'>"); int id=rs.getInt; out.println(id); out.println("</td>"); out.println("<form method='post' action='add.jsp' target='cart'>"); out.println("<input type='hidden' name='name' value="+name+">"); out.println("<input type='hidden' name='type' value="+type+">"); out.println("<input type='hidden' name='price' value="+price+">"); out.println("<input type='hidden' name='off' value="+off+">"); out.println("<input type='hidden' name='id' value="+id+">"); out.println("<td bgcolor='#CCCCCC'><input type='text' name='number' size=4></td>"); out.println("<td bgcolor='#CCCC33'><input type='submit' value='add'></td>"); out.println("</form>"); out.println("</tr>"); count++; } rs.close(); count--; out.println("Total Search Records:"+count); out.println("</table>"); stmt.close(); conn.close(); } catch (Exception fe) { out.println(fe.getMessage()); } } } |
2.Re:做servlet遇到一个莫名其妙的问题。有源码 [Re: goodluckwhh] | Copy to clipboard |
Posted by: k704790 Posted on: 2005-12-02 15:40 把这个“PrintWriter out=new PrintWriter(resp.getOutputStream());”改成“PrintWriter out = resp.getPrintWriter()"看行不行. |
3.Re:做servlet遇到一个莫名其妙的问题。有源码 [Re: goodluckwhh] | Copy to clipboard |
Posted by: goodluckwhh Posted on: 2005-12-02 22:20 我试了,不可以,该了之后编译都通不过了 不过还是谢谢你 |
4.Re:做servlet遇到一个莫名其妙的问题。有源码 [Re: goodluckwhh] | Copy to clipboard |
Posted by: ntshenwh Posted on: 2005-12-04 15:51 好像是没有加 out.println("<html>");........out.println("</html>") |
5.Re:做servlet遇到一个莫名其妙的问题。有源码 [Re: goodluckwhh] | Copy to clipboard |
Posted by: ntshenwh Posted on: 2005-12-04 15:55 另外 1)可以不要用 service 方法,用 public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException 代替它 2) public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException 用 PrintWriter out = response.getWriter(); 代替 3) resp.setContentType("text/html"); 用 resp.setContentType("text/html;charset=gb2312"); 代替 good luck! |
6.Re:做servlet遇到一个莫名其妙的问题。有源码 [Re: goodluckwhh] | Copy to clipboard |
Posted by: ntshenwh Posted on: 2005-12-04 15:56 sorry, should be: 2) PrintWriter out=new PrintWriter(resp.getOutputStream()); 用 PrintWriter out = response.getWriter(); 代替 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |