2015-09-22 87 views
0

之前這是我在Jsp中的代碼,它應該通過從我的數據庫中獲取所有項目來打印表格。但實際情況是,首先顯示jsp代碼段的所有輸出。JSP腳本的輸出顯示在HTML

<%@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> 

這是GlassFish服務器在NetBeans輸出

<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> 

我運行這一點。 請告訴我如何糾正它?

+0

1)使用'out'隱式對象(見伊薩姆的答案); 2)**不要使用scriptlets **,使用[MVC模式](http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/),或者使用至少[SQL標記庫](http://www.devmanuals.com/tutorials/java/jsp/jstl/sql/)。 –

回答

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> 
+0

謝謝你。 爲我工作 – user1519783

+0

樂於幫助,並歡迎Stack Overflow。如果此答案或任何其他人解決了您的問題,請將其標記爲已接受。正如在評論中提供的建議,避免使用scriptlet並使用MVC模式 –