我从我的oracle servlet得到一个空白屏幕

时间:2013-02-14 03:23:53

标签: java oracle servlets

这是我创建的servlet,当我运行它时它只给我一个空白的屏幕:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;

public class salesWOBriensrv extends HttpServlet {



/**
 *
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    PrintWriter out = response.getWriter();
    response.setContentType("text/html");
    Connection connection = null;

    try{

        Class.forName("oracle.jdbc.driver.OracleDriver");

        connection = DriverManager.getConnection("jdbc:oracle:thin:@xxxx.xxxx.xxxx:1521:xxxx","xxxx","xxxxx");

        Statement stmt = connection.createStatement();

        ResultSet rs = stmt.executeQuery("SELECT * FROM sales ORDER BY sale_amount, sale_date");

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Step 2 - Java Servlet for SALES Data/title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Step 2 - Java Servlet for SALES Data</h1>");
        out.println("<table border='1'>");
        out.println("<b><th><tr><td>Sales ID</td>" +
                               "<td>Home ID</td>" +
                               "<td>Customer ID</td>" +
                               "<td>Agent ID</td>" +
                               "<td>Sale Date</td>" +
                               "<td>Sale Amount</td>" +
                               "<td></td>Contract ID</tr></th></b>");
        while (rs.next()) {

           // Get the columns from the current row of the result set
           String saleID = rs.getString(1);
           String homeID = rs.getString(2);
           String customerID = rs.getString(3);
           String agentID = rs.getString(4);
           String saleDate = rs.getString(5);
           String saleAmnt = rs.getString(6);
           String contractID = rs.getString(7);

           out.println("<tr><td>" + saleID + "</td>" +
                           "<td>" + homeID + "</td>" +
                           "<td>" + customerID + "</td>" +
                           "<td>" + agentID + "</td>" +
                           "<td>" + saleDate + "</td>" +
                           "<td>" + saleAmnt + "</td>" +
                           "<td>" + contractID + "</td></tr>");
        }

        out.println("</table>");
        out.println("</body>");
        out.println("</html>");

     }


    catch (Exception e){
     out.println("<html>");   
     out.println("<body>"); 
     out.println("Error");  
     out.println("</body>");   
    }

}




/**
 * Handles the HTTP
 * <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */


protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

   doGet (request, response);

}

}

我用x替换了我的连接字符串,但我知道我拥有的是正确的。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

我的猜测PrintWriter没有刷新内容。 你能试试吗

out.flush();
代码末尾的

答案 1 :(得分:0)

在servlet代码中尝试此命令以创建PrintWriter对象

response.setContentType("text/html");
PrintWriter out = response.getWriter();