JSP脚本的输出显示在HTML之前

时间:2015-09-22 12:22:31

标签: jsp

这是我在Jsp中的代码,它应该通过从我的数据库中获取所有项目来打印表。但实际发生的是首先显示jsp片段的所有输出。

<%@page import="java.io.PrintWriter"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="pos.dbconnection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>View All the Items</title>
</head>
<body>
    <form action='viewitems.jsp'>
        <table border='1'>
    <% 
         Connection con = dbconnection.getconnection();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from panch.raw;");
        PrintWriter pw = response.getWriter();
        pw.print("<tr>"+
        "<th>Raw_Id</th>"+
        "<th>Raw_Name</th>"+
        "<th>Raw_Quantity</th>"+
        "<th>Raw_CostPrice</th>"+
        "</tr>");


        while(rs.next())
        { 
        pw.print("<tr>");
        pw.print("<td>"+rs.getString("raw_id")+"</td>");
        pw.print("<td>"+rs.getString("raw_name")+"</td>");
        pw.print("<td>"+rs.getString("raw_quantity")+"</td>");
        pw.print("<td>"+rs.getString("raw_cost")+"</td>");
         pw.print("<td><input name='id' value='"+rs.getString("raw_id")+
                 "' hidden='true'/> <input type='submit' value='Edit'/>"+
                 "</td>");
        pw.print("</tr>");
        } %>
    </table> 

  </form>
</body>
</html>

这是OUtput

<tr>
 <th>Raw_Id</th><th>Raw_Name</th>
 <th>Raw_Quantity</th> 
 <th>Raw_CostPrice</th>
</tr>
<tr>
  <td>1</td>
  <td>Ghee</td>
  <td>5000</td>
  <td>80</td>
  <td><input name='id' value='1' hidden='true'/> 
      <input type='submit' value='Edit'/></td>
 </tr>
 <!DOCTYPE html>
 <html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>View All the Items</title>
</head>
<body>
    <form action='viewitems.jsp'>
        <table border='1'>

    </table> 

  </form>
</body>
</html>

我在Netbeans的Glassfish Server中运行它。 请告诉我如何纠正它?

1 个答案:

答案 0 :(得分:1)

尝试使用out.println,如下所示

<%@page import="java.io.PrintWriter"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="pos.dbconnection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>View All the Items</title>
</head>
<body>
    <form action='viewitems.jsp'>
        <table border='1'>
    <% 
         Connection con = dbconnection.getconnection();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from panch.raw;");

        out.println("<tr>"+
        "<th>Raw_Id</th>"+
        "<th>Raw_Name</th>"+
        "<th>Raw_Quantity</th>"+
        "<th>Raw_CostPrice</th>"+
        "</tr>");


        while(rs.next())
        { 
        out.println("<tr>");
        out.println("<td>"+rs.getString("raw_id")+"</td>");
        out.println("<td>"+rs.getString("raw_name")+"</td>");
        out.println("<td>"+rs.getString("raw_quantity")+"</td>");
        out.println("<td>"+rs.getString("raw_cost")+"</td>");
         out.println("<td><input name='id' value='"+rs.getString("raw_id")+
                 "' hidden='true'/> <input type='submit' value='Edit'/>"+
                 "</td>");
        out.println("</tr>");
        } %>
    </table> 

  </form>
</body>
</html>