之前這是我在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>
我運行這一點。 請告訴我如何糾正它?
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/)。 –