user1519783
user1519783

Reputation: 23

Output of JSP Script is displayed before the HTML

This is my Code in Jsp which should print a table by getting all the items from my database. But what actually happens is that all the output of the jsp snippet is displayed first.

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

This is the 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>

I am running this in Glassfish Server In Netbeans. Please tell me how to rectify it?

Upvotes: 0

Views: 229

Answers (1)

GUISSOUMA Issam
GUISSOUMA Issam

Reputation: 2582

Try to use out.println, like below

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

Upvotes: 1

Related Questions