1

这是我创建的 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 替换了我的连接字符串,但我知道我所拥有的是正确的。任何帮助将不胜感激。

4

2 回答 2

1

我猜 PrintWriter 没有刷新内容。你能试试这个

out.flush();

在您的代码末尾

于 2013-02-16T18:31:12.470 回答
0

在您的 servlet 代码中尝试此顺序以创建PrintWriter对象

response.setContentType("text/html");
PrintWriter out = response.getWriter();
于 2013-02-14T04:51:03.237 回答