鍍金池/ 問答/Java  Linux  HTML/ java servlet 頁面顯示不全。

java servlet 頁面顯示不全。

在學習java ee的時候。用事例的代碼跑項目的時候出現(xiàn)顯示不全的問題,重啟電腦和服務器都無果。找了很久的毛病都沒有找到,特來請教大家,還望大家能夠解答,謝謝!
下面上代碼:

if(isUserExist){
                    req.setAttribute("login", loginValue);
                    try {
                        getOrderList(req, resp);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    displayMyStocklistPage(req, resp);
                    
                }else{
                    displayErrorPage(req,resp);
                }

在這個servlet中調(diào)用了(displayMystocklistPage )這兩個方法來畫界面。
下面是方法內(nèi)容:

public void displayMyStocklistPage(HttpServletRequest req, HttpServletResponse res) throws IOException {
        ArrayList list = (ArrayList) req.getAttribute("list"); // resp.sendRedirect(req.getContextPath()+"/MyStockList");

        boolean isItemOut = false;
        PrintWriter out = res.getWriter();
        //用本地的out查看內(nèi)容
        //PrintStream out = null ;     // 聲明打印流對象
        // 如果現(xiàn)在是使用FileOuputStream實例化,意味著所有的輸出是向文件之中  
       // out = new PrintStream(new FileOutputStream(new File("/Users/insomnialee/Downloads/Sample-20171227/sample2/web/test.txt"))) ;

        out.println("<html><head>");
        out.println("<script src=\"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js\"></script>\n" +
                "    <style>\n" +
                "        table, th , td {\n" +
                "            border: 1px solid grey;\n" +
                "            border-collapse: collapse;\n" +
                "            padding: 5px;\n" +
                "        }\n" +
                "        table tr:nth-child(odd) {\n" +
                "            background-color: #f1f1f1;\n" +
                "        }\n" +
                "        table tr:nth-child(even) {\n" +
                "            background-color: #ffffff;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>");

        out.println("<table width='650' border='0' >");
        out.println("<tr>");
        out.println("<td width='650' height='80' background='" + req.getContextPath() + "/image/top.jpg'>&nbsp;</td>");
        out.println("</tr>");
        out.println("</table>");
        out.println("<p>Welcome " + req.getAttribute("login") + "</p>");

        out.println("My Order List:  ");
        out.println("<br>");


        out.println("<table>");
        out.println("<tr>"+
                "<td>"+"日期"+"</td>"+
                "<td>"+"id"+"</td>"+
                "<td>"+"商品名稱"+"</td>"+
                "<td>"+"數(shù)量"+"</td>"+
                "<td>"+"總價"+"</td>"+
                "<td>"+"商品狀態(tài)"+"</td>"+
                "</tr>");
        for (int i = 0; i < list.size(); i++) {
            Order order = (Order) list.get(i);
            out.println("<tr>"+
                    "<td>"+order.getDate().toString()+"</td>"+
                    "<td>"+order.getUid()+"</td>"+
                    "<td>"+order.getCname()+"</td>"+
                    "<td>"+order.getCnum()+"</td>"+
                    "<td>"+order.getPrice()+"</td>"
                    );
            if(order.getIsout()==0){
                isItemOut = true;
                out.println("<td>"+"-缺貨中-"+"</td>");
            }else{
                out.println("<td>"+"-正常-"+"</td>");
            }
            out.println("</tr>");
        }
        out.println("</table>");

//        out.println("</p>");
        if(isItemOut){
            out.println("<p style=\"color:red\"> 您訂單中有部分商品缺貨! </p>");
        }

        out.println("Click <a href='" + res.encodeURL(req.getRequestURI()) + "'>here</a> to reload this page.<br>");
        out.println("<form method='GET' action='" + res.encodeURL(req.getContextPath() + "/Login") + "'>");
        out.println("<input value='Logout'>");
        out.println("</form>");
        out.println("<p>Servlet is version @version@</p>");
        out.println("</body></html>");
        //displayout page
        //out.close();
    }

最后展示出來的界面是這樣的
//插圖老是失敗,用文字描述下吧,最后的界面中沒有代碼最后<input>和<p>這兩個標簽??蛻舳私邮盏降膆tml中只有一個空的<form>,而在我測試的時候發(fā)現(xiàn),刪去后面的一些展示代碼。前面本來能夠正常顯示的甚至還會變得不正常顯示。我也做了將輸出輸出到txt然后在當成html文件打開的辦法,這樣測試出來的界面是正常的。所以懷疑是通信有大小限制,在out中查看的時候,又看到?jīng)]有達到limit的限制值(8192),,希望過來人能夠指點迷津,,謝謝?。?/p>

回答
編輯回答
雨蝶
flush public abstract void flush() throws IOExceptionFlush
Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.
The method may be invoked indirectly if the buffer size is exceeded.

Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown.

可能是字符串超出緩存區(qū)了,自己算算,在合適的地方調(diào)用out.flush()吧.

2018年4月7日 13:46